-
Notifications
You must be signed in to change notification settings - Fork 20
/
SetEdgeDependencies.ps1
166 lines (138 loc) · 5.32 KB
/
SetEdgeDependencies.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#####-----------------------------------------------------------------------------------------#####
#region Functions
# Create a function to connect to the SolarWinds Information Service (SWIS)
Function Set-SwisConnection {
Param(
[ Parameter( Mandatory = $true, HelpMessage = "What SolarWinds server are you connecting to (Hostname or IP)?" ) ] [ string ] $SolarWindsServer,
[ Parameter( Mandatory = $true, HelpMessage = "Do you want to use the credentials from PowerShell (Trusted), or a new login (Explicit)?" ) ] [ ValidateSet( 'Trusted', 'Explicit' ) ] [ string ] $ConnectionType
)
# Connect to SWIS
IF ( $ConnectionType -eq 'Trusted' ) {
$swis = Connect-Swis -Trusted -Hostname $SolarWindsServer
}
ELSE {
$creds = Get-Credential -Message "Please provide a Domain or Local Login for SolarWinds"
$swis = Connect-Swis -Credential $creds -Hostname $SolarWindsServer
}
RETURN $swis
}
Function Write-Log {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline)]
[string]
$Message
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$Line = "$Stamp $Message"
Add-Content $logfile -Value $Line
Write-Output $Line
}
#endregion Functions
#####-----------------------------------------------------------------------------------------#####
clear-host
$ScriptName = $MyInvocation.MyCommand.Name
$Logfile = "D:\Scripts\$ScriptName.log"
$hostname = "localhost" #Read-Host -Prompt "Hostname or IP Address of your SolarWinds server"
$swis = Set-SwisConnection -SolarWindsServer $hostname -ConnectionType trusted
"`n`n`nBegin Execution" | Write-Log
"hostname $hostname" | Write-Log
<#------------- ACTUAL SCRIPT -------------#>
##### Phase 1 #######
"Finding Dependencies group" | write-Log
$depParentID = Get-SwisData $swis "select containerid from orion.container where name = 'Dependencies'"
"Checking for Edge groups" | Write-Log
$edgeWANGroups = Get-swisdata $swis @"
select ncp.site, count(*) as wans
from orion.nodes n
join orion.NodesCustomProperties ncp on ncp.nodeid=n.nodeid
where n.caption like '%wan%'
group by ncp.site
having count(*) > 1
"@
foreach($wan in $edgeWANGroups) {
"Working with $($wan.site)" | write-Log
$parent = Get-SwisData $swis @"
select top 1 'EDGE $($wan.site)' as Name, c.ContainerID
from orion.nodes n
left join orion.Container c on n.caption like '%' and c.name = 'EDGE $($wan.site)'
"@
if(!$parent.ContainerID){
"Creating Edge Group" | Write-Log
$members = @(
@{ Name = "$($wan.site) WAN NODES"; Definition = "filter:/Orion.Nodes[Contains(Caption,'WAN') AND CustomProperties.SIte='$($wan.site)']" }
)
$parent.Name = "EDGE $($wan.site)"
$parent.ContainerID = (invoke-swisverb $swis "orion.container" "CreateContainerWithParent" @(
$depParentID,
"EDGE $($wan.site)",
"Core",
60,
0,
"",
"true",
([xml]@("<ArrayOfMemberDefinitionInfo xmlns='http://schemas.solarwinds.com/2008/Orion'>",
[string]($members |% {
"<MemberDefinitionInfo><Name>$($_.Name)</Name><Definition>$($_.Definition)</Definition></MemberDefinitionInfo>"
}
),
"</ArrayOfMemberDefinitionInfo>"
)).DocumentElement
)).innertext
}
$edgeID = "C:"+$parent.ContainerID
"Getting list of children of $($wan.site) EDGE" | Write-Log
$children = Get-SwisData $swis @"
select n.caption, ncp.uri, ncp.Edgeid
from orion.nodes n
join orion.NodesCustomProperties ncp on ncp.nodeid=n.nodeid
where n.caption not like '%WAN%'
and ncp.site = '$($wan.site)'
"@
foreach($child in $children) {
"Working with $($child.caption)" | Write-Log
if($child.edgeid -eq $null) {
" Setting EdgeID to $edgeID" | Write-Log
Set-SwisObject $swis $child.uri -Properties @{ EdgeID = $edgeID }
} else {
" EdgeID already set to $($child.edgeid)" | Write-Log
}
}
}
"Checking for single nodes edges" | Write-Log
$edgeWANNodes = Get-swisdata $swis @"
select ncp.site, count(*) as wans
from orion.nodes n
join orion.NodesCustomProperties ncp on ncp.nodeid=n.nodeid
where n.caption like '%wan%'
group by ncp.site
having count(*) = 1
"@
foreach($wan in $edgeWANNodes) {
$parent = Get-SwisData $swis @"
select Caption as Name, n.nodeid
from orion.nodes n
where n.caption like '%wan%' and n.CustomProperties.site = '$($wan.site)'
"@
$edgeID = "N:"+$parent.nodeid
"Getting list of children of $($wan.site) EDGE" | Write-Log
$children = Get-SwisData $swis @"
select n.caption, ncp.uri, ncp.Edgeid
from orion.nodes n
join orion.NodesCustomProperties ncp on ncp.nodeid=n.nodeid
where n.caption not like '%WAN%'
and ncp.site = '$($wan.site)'
"@
foreach($child in $children) {
"Working with $($child.caption)" | Write-Log
if($child.edgeid -eq $null) {
" Setting EdgeID to $edgeID" | Write-Log
Set-SwisObject $swis $child.uri -Properties @{ EdgeID = $edgeID }
} else {
" EdgeID already set to $($child.edgeid)" | Write-Log
}
}
}
"`nFinished" | Write-Log
"Errors below" | Write-Log
$Error | Write-Log