-
Notifications
You must be signed in to change notification settings - Fork 2
/
Update-OrionServerNodeIds.ps1
87 lines (75 loc) · 4.59 KB
/
Update-OrionServerNodeIds.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<##################################################
Update-OrionServerNodeIds.ps1
This script will update the Orion.OrionServers elements with the matching NodeIDs
It prompts for confirmation on each update since there's a chance there will be a mismatch.
---Tested with Core 2020.2.6 HF2---
##################################################>
if ( -not ( $SwisConnection ) ) {
$SwisHost = Read-Host -Prompt "Provide the IP or FQDN of your Orion server"
if ( -not ( $SwisCreds ) ) {
$SwisCreds = Get-Credential -Message "Enter your Orion credentials for $SwisHost"
}
if ( $SwisHost -and $SwisCreds ) {
$SwisConnection = Connect-Swis -Hostname $SwisHost -Credential $SwisCreds
}
}
$MissingNodeIds = Get-SwisData -SwisConnection $SwisConnection -Query "SELECT Uri, HostName FROM Orion.OrionServers WHERE IsNull(NodeID, 0) = 0"
if ( $MissingNodeIds ) {
ForEach ( $MissingNodeId in $MissingNodeIds ) {
Write-Host "Checking matches for '$( $MissingNodeId.HostName )'"
$PossibleMatches = Get-SwisData -SwisConnection $SwisConnection -Query "SELECT NodeID, Caption, DNS, SysName, IPAddress FROM Orion.Nodes WHERE Caption LIKE '%$( $MissingNodeId.HostName )%' OR DNS LIKE '%$( $MissingNodeId.HostName )%' OR SysName LIKE '%$( $MissingNodeId.HostName )%'"
if ( $PossibleMatches.Count -eq 1 ) {
# Single match
Write-Host "`tFound a potential match:"
$PossibleMatches | Format-Table
# Build menu for choice
$Yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList ( '&Yes', "Set NodeID for $( $MissingNodeId.HostName ) to $( $PossibleMatches.NodeID )" )
$No = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList ( '&No', "Make no changes")
$Choices = [System.Management.Automation.Host.ChoiceDescription[]]($Yes, $No)
$Title = "Update Orion.Servers Entry"
$Message = "Do you want to update entry for '$( $MissingNodeId.HostName )' in Orion.OrionServers with NodeID: $( $PossibleMatches.NodeID )?"
$Response = $Host.Ui.PromptForChoice($Title, $Message, $Choices, 1)
# Check to see if we said "Yes" (Response 0)
if ( $Response -eq 0 ) {
Write-Host "We'd run the update now" -ForegroundColor Green
Write-Host 'Command Code: Set-SwisObject -SwisConnection $SwisConnection -Uri $( $MissingNodeId.Uri ) -Properties @{ NodeId = $PossibleMatches.NodeID }' -ForegroundColor Green
Write-Host "Command that's sent: Set-SwisObject -SwisConnection `$SwisConnection -Uri $( $MissingNodeId.Uri ) -Properties `@{ NodeId = $( $PossibleMatches.NodeID ) }" -ForegroundColor Green
}
else {
Write-Host "I guess not you aren't interested in doing the update" -ForegroundColor Red
}
}
elseif ( $PossibleMatches.Count -gt 1 ) {
# Multiple matches
Write-Host "`tFound multiple potential matches:"
$PossibleMatches | Format-Table
$Response = $null
do {
if ( -not $ResponseOk ) {
if ( $Response ) {
Write-Error -Message "'$Response' is an invalid NodeID. Please enter a Node ID from the below table."
}
$PossibleMatches | Format-Table -AutoSize
}
Write-Host "Enter the NodeID you'd like to assign to '$( $MissingNodeID.HostName )' in Orion.OrionServers " -NoNewLine
$Response = Read-Host -Prompt "or enter 'S' to skip"
$ResponseOk = $Response -in ( $PossibleMatches.NodeID ) -or $Response -eq 's'
} until ( $ResponseOk )
if ( $Response.ToLower() -ne 's' ) {
Write-Host "We'd run the update now" -ForegroundColor Green
Write-Host 'Command Code: Set-SwisObject -SwisConnection $SwisConnection -Uri $( $MissingNodeId.Uri ) -Properties @{ NodeId = $Response }' -ForegroundColor Green
Write-Host "Command that's sent: Set-SwisObject -SwisConnection `$SwisConnection -Uri $( $MissingNodeId.Uri ) -Properties `@{ NodeId = $Response }" -ForegroundColor Green
}
else {
Write-Host "I guess not." -ForegroundColor Red
}
}
else {
# No matches
Write-Error -Message "Found no appropriate matches. Are you monitoring your Orion servers?"
}
}
}
else {
Write-Warning -Message "All Orion Server objects have a matching NodeID"
}