-
Notifications
You must be signed in to change notification settings - Fork 20
/
CloneApplicationViews.ps1
120 lines (89 loc) · 4.12 KB
/
CloneApplicationViews.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
<#------------- CONNECT TO SWIS -------------#>
# load the snappin if it's not already loaded (step 1)
if (!(Get-PSSnapin | Where-Object { $_.Name -eq "SwisSnapin" })) {
Add-PSSnapin "SwisSnapin"
}
#define target host and credentials
$hostname = 'localhost'
#$user = "admin"
#$password = "password"
# create a connection to the SolarWinds API
#$swis = connect-swis -host $hostname -username $user -password $password -ignoresslerrors
$swis = Connect-Swis -Hostname $hostname -Trusted
<#------------- ACTUAL SCRIPT -------------#>
# check if application has a view already
$needviews = @"
select distinct toupper(ncp.applications) as applications, c.ContainerID, c.name, v.ViewID, v.ViewTitle, v.ViewGroupName
from orion.NodesCustomProperties ncp
left join orion.Container c on c.name like ncp.applications
left join orion.views v on v.viewtitle like ('Application - '+ c.name) or (v.ViewGroupName like ('Application - '+ c.name) and v.ViewGroupPosition=1)
where ncp.applications is not null and ncp.applications not in ('','N/A') and ncp.applications not like '%,%'
and v.viewid is null
"@
$views = get-swisdata $swis $needviews
$Gettemplate = @"
SELECT ViewID
FROM Orion.Views
where viewtitle = '_Application Template'
"@
$viewtemplateid = get-swisdata $swis $Gettemplate
# clone applications view template
foreach($view in $views)
{
"Creating view for $($view.applications)"
invoke-swisverb $swis "Orion.Views" "CloneView" @(
#View to clone
"$viewtemplateid",
#name to give
"Application - $($view.applications)"
)
$getnewview = @"
SELECT ViewID
FROM Orion.Views
where viewtitle = 'Application - $($view.applications)'
"@
$newviewid = get-swisdata $swis $getnewview
#copy resources from template to new view
invoke-swisverb $swis "Orion.Views" "CloneViewContents" @(
#source view
"$viewtemplateid",
#destination view
"$newviewid"
)
}
########## Group Limitations ###########
$uriroot = get-swisdata $swis "SELECT SettingValue FROM Orion.WebSettings where settingname='SwisUriSystemIdentifier'"
#check for existing limitation
$limittype = get-swisdata $swis "SELECT LimitationTypeID FROM Orion.LimitationTypes where name = 'Group of Groups'"
$needlimits = @"
select distinct toupper(ncp.applications) as applications, c.ContainerID, c.name, v.ViewID, v.ViewTitle, v.ViewGroupName, l.LimitationID
from orion.NodesCustomProperties ncp
left join orion.Container c on c.name like ncp.applications
left join orion.views v on v.viewtitle like ('Application - '+ c.name) or (v.ViewGroupName like ('Application - '+ c.name) and v.ViewGroupPosition=1)
left join orion.Limitations l on l.WhereClause like ('% '+tostring(c.ContainerID)+')%') and LimitationTypeID=$limittype
where ncp.applications is not null and ncp.applications not in ('','N/A') and ncp.applications not like '%,%'
and l.limitationid is null
"@
$limitations = get-swisdata $swis $needlimits
#create application group based limitation
foreach($limit in $limitations)
{
"Creating limitation for $($limit.applications)"
invoke-swisverb $swis "Orion.Limitations" "CreateLimitation" @( "$limittype",$null,@("$($limit.containerid)"),$null,$null )
}
$needtoapply = @"
select distinct toupper(ncp.applications) as applications, c.ContainerID, c.name, v.ViewID, v.ViewTitle, v.ViewGroupName, l.LimitationID
from orion.NodesCustomProperties ncp
left join orion.Container c on c.name like ncp.applications
left join orion.views v on v.viewtitle like ('Application - '+ c.name) or (v.ViewGroupName like ('Application - '+ c.name) and v.ViewGroupPosition=1)
left join orion.Limitations l on l.WhereClause like ('% '+tostring(c.ContainerID)+')%') and LimitationTypeID=19
where ncp.applications is not null and ncp.applications not in ('','N/A') and ncp.applications not like '%,%'
and limitationid is not null
"@
$toapply = get-swisdata $swis $needtoapply
#create application group based limitation
foreach($view in $toapply)
{
#apply limitation to view
Set-SwisObject $swis -Uri "swis://$uriroot/Orion/Orion.Views/ViewID=$($view.viewid)" -Properties @{"limitationid"="$($view.limitationid)"}
}