-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateMPandCustomGroup_v1.0.ps1
228 lines (176 loc) · 11.3 KB
/
CreateMPandCustomGroup_v1.0.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# --- [CreateMPandCustomGroup_v1.0] PowerShell Script ---
#
# Author(s): Ryan Irujo
# Inception: 11.23.2013
# Last Modified: 11.26.2013
#
# Description: Creates a Custom Management Pack and a Custom Group. Additionally, it adds as many Monitored Hosts to the Group
# based upon the number provided to the Script.
#
# Changes: 11.26.2013 - [R. Irujo]
# - Added functionality whereby if special characters were found in the Management Pack ID, Management Pack Name or
# any Monitored Hosts, they will be removed before the Script continues.
#
#
#
# Syntax: ./CreateMPandCustomGroup_v1.0 <Management_Server> <MP_ID> <MP_Name> <MP_DisplayName> <Monitored_Hosts>
#
# Example: ./CreateMPandCustomGroup_v1.0 SCOMMS01.fabrikam.local "Test.Custom.Group.101" "Test.Custom.Group.101" "Test Custom Group 101" ("TestServer101","TestServer102")
param($ManagementServer,$ManagementPackID,$ManagementPackName,$ManagementPackDisplayName,[array]$MonitoredHosts)
# [---START---] Try-Catch Wrapper for Entire Script.
try {
Clear-Host
# Importing SCOM SDK DLL Files.
Import-Module "C:\Program Files\System Center 2012\Operations Manager\Console\SDK Binaries\Microsoft.EnterpriseManagement.Core.dll"
Import-Module "C:\Program Files\System Center 2012\Operations Manager\Console\SDK Binaries\Microsoft.EnterpriseManagement.OperationsManager.dll"
Import-Module "C:\Program Files\System Center 2012\Operations Manager\Console\SDK Binaries\Microsoft.EnterpriseManagement.Runtime.dll"
# Checking Parameter Values.
if (!$ManagementServer) {
Write-Host "A Management Server Name must be provided, i.e. - SCOMMS01.fabrikam.local."
exit 2;
}
if (!$ManagementPackID) {
Write-Host "A Management Pack ID must be provided, i.e. - custom.service.monitor.mp01. The Management Pack ID can be the same as the Management Pack Name."
exit 2;
}
if (!$ManagementPackName) {
Write-Host "A Management Pack Name must be provided, i.e. - custom.service.monitor.mp01. The Management Pack Name can be the same as the Management Pack ID."
exit 2;
}
if (!$ManagementPackDisplayName) {
Write-Host "A Management Pack Display Name must be provided, i.e. - Custom Service Monitor MP01."
exit 2;
}
if (!$MonitoredHosts) {
Write-Host "The Name of the Hosts (NetBIOS or FQDN) you want to add to the Custom Group must be provided , i.e. - (`"TestServer101`",`"TestServer102`")."
exit 2;
}
Write-Host "Management Server: $($ManagementServer)"
Write-Host "Management Pack ID: $($ManagementPackID)"
Write-Host "Management Pack Name: $($ManagementPackName)"
Write-Host "Management Pack Display Name: $($ManagementPackDisplayName)"
Write-Host "Monitored Hosts: $($MonitoredHosts)"
# Connecting to the SCOM Management Group
Write-Host "Connecting to the SCOM Management Group"
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer)
# Replacing Special Characters that may appear in the Management Pack ID.
If ($ManagementPackID -match "[!@#$%^&*()`_-]") {
$ManagementPackID = $ManagementPackID -replace "[!@#$%^&*()`_-]",""
}
# Replacing Special Characters that may appear in the Management Pack Name.
If ($ManagementPackName -match "[!@#$%^&*()`_-]") {
$ManagementPackName = $ManagementPackName -replace "[!@#$%^&*()`_-]",""
}
# Renaming Monitored Hosts that contain Underscore and/or Dash Characters in their Names.
Foreach ($MonitoredHost in $MonitoredHosts) {
If ($MonitoredHost -match "[_-]") {
$MonitoredHosts += $MonitoredHost -replace "[_-]",""
}
}
# Removing old Monitored Hosts names that contain Underscore and/or Dash Characters in their Names.
$MonitoredHosts = $MonitoredHosts | Where-Object {$_ -notmatch "[_-]"}
# Determining the GUID of the Monitored Hosts in SCOM.
$HostGUIDs = $null
Foreach ($MonitoredHost in $MonitoredHosts) {
$ObjectCriteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringObjectGenericCriteria("FullName='Microsoft.Windows.Computer:$($MonitoredHost)'")
$HostGUID = ($MG.GetMonitoringObjects($ObjectCriteria)).Id.ToString()
$HostGUIDs += "<MonitoringObjectId>$HostGUID</MonitoringObjectId>`n"
}
# Determining if the Management Pack exists based upon its Display Name using a String Query.
Write-Host "Determining if Management Pack - [$($ManagementPackDisplayName)] already exists."
$MPQuery = "DisplayName = '$($ManagementPackDisplayName)'"
$MPCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackCriteria($MPQuery)
$MP = $MG.GetManagementPacks($MPCriteria)[0]
If ($MP.Count -eq "0") {
Write-Host "Management Pack - [$($ManagementPackDisplayName)] was not found in SCOM. Management Pack and Custom Group Creation will now continue."
}
Else {
Write-Host "Management Pack - [$($ManagementPackDisplayName)] already exists in SCOM. Script is now exiting."
exit 2;
}
# Starting the Process of Creating a New Management Pack.
Write-Host "Creating new [Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore] object"
$MPStore = New-Object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackFileStore
Write-Host "Creating new [Microsoft.EnterpriseManagement.Configuration.ManagementPack] object"
$MP = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPack($ManagementPackID, $ManagementPackName, (New-Object Version(1, 0, 0)), $MPStore)
Write-Host "Importing New Management Pack"
$MG.ImportManagementPack($MP)
Write-Host "Retrieving Newly Created Management Pack"
$MP = $MG.GetManagementPacks($ManagementPackID)[0]
Write-Host "Setting Management Pack Display Name."
$MP.DisplayName = $ManagementPackDisplayName
Write-Host "Setting Management Pack Description."
$MP.Description = "This Management Pack was Auto-Generated by the [CreateMPandCustomGroup_v1.0.ps1] Script."
# Getting System Library Reference
$SystemLibrary = "System.Library"
$SystemLibraryRef = $MG.GetManagementPacks($SystemLibrary)[0]
$SystemLibraryAlias = "System"
# Getting Group Library Reference
$GroupLibrary = "Microsoft.SystemCenter.InstanceGroup.Library"
$GroupLibraryRef = $MG.GetManagementPacks($GroupLibrary)[0]
$GroupLibraryAlias = "MicrosoftSystemCenterInstanceGroupLibrary7585010"
# Adding System Library Reference to Management Pack
$MPRefSystemLibrary = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackReference($SystemLibraryRef)
$MP.References.Add($SystemLibraryAlias, $MPRefSystemLibrary)
# Adding Group Library Reference to Management Pack
$MPRefGroupLibrary = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackReference($GroupLibraryRef)
$MP.References.Add($GroupLibraryAlias, $MPRefGroupLibrary)
Write-Host "New References Added to Management Pack - [$($MP.DisplayName)]."
# Creating Custom Group Class - Microsoft.SystemCenter.InstanceGroup
$CustomComputerGroupClass = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackClass($MP,("CustomComputerGroup_"+[Guid]::NewGuid().ToString().Replace("-","")),"Public")
$CustomComputerGroupBase = $MG.EntityTypes.GetClasses("Name='Microsoft.SystemCenter.InstanceGroup'")[0]
$CustomComputerGroupClass.Base = $CustomComputerGroupBase
$CustomComputerGroupClass.Singleton = $true
$CustomComputerGroupClass.Hosted = $false
$CustomComputerGroupClass.DisplayName = "$($ManagementPackDisplayName), Custom Computer Group"
# Creating a New Discovery Rule for the Custom Group Class - Microsoft.SystemCenter.ComputerGroup
$CustomComputerGroupClassDiscovery = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscovery($MP,($CustomComputerGroupClass.ToString()+"_Discovery_Rule"))
$CustomComputerGroupClassDiscovery.Category = "Discovery"
$CustomComputerGroupClassDiscovery.DisplayName = "$($ManagementPackDisplayName) - Discovery"
$CustomComputerGroupClassDiscovery.Description = "Discovery Rule for $($ManagementPackDisplayName)"
# Creating and Adding the Discovery Target for the CustomComputerGroupClass
$CustomComputerGroupClassDiscovery.Target = $CustomComputerGroupClass
# Creating and Adding the Discovery Relationship using the CustomComputerGroupClass
$CustomComputerGroupClass_DiscoveryRelationship = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscoveryRelationship
$CustomComputerGroupClass_DiscoveryRelationship_TypeID = $MG.EntityTypes.GetRelationshipClasses("Name='Microsoft.SystemCenter.InstanceGroupContainsEntities'")[0]
$CustomComputerGroupClass_DiscoveryRelationship.set_TypeID($CustomComputerGroupClass_DiscoveryRelationship_TypeID)
$CustomComputerGroupClassDiscovery.DiscoveryRelationshipCollection.Add($CustomComputerGroupClass_DiscoveryRelationship)
# Creating and Adding the DataSource for the Discovery Rule of the CustomComputerGroupClass
$CustomComputerGroupClassDiscovery_DS = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModule($CustomComputerGroupClassDiscovery,"GroupPopulationDataSource")
$CustomComputerGroupClassDiscovery_DS_ModuleType = $MG.GetMonitoringModuleTypes("Microsoft.SystemCenter.GroupPopulator")[0]
$CustomComputerGroupClassDiscovery_DS.TypeID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackDataSourceModuleType]$CustomComputerGroupClassDiscovery_DS_ModuleType
$CustomComputerGroupClassDiscovery_DS.Configuration = "<RuleId>`$MPElement$</RuleId>
<GroupInstanceId>`$MPElement[Name=`"$($CustomComputerGroupClass)`"]$</GroupInstanceId>
<MembershipRules>
<MembershipRule>
<MonitoringClass>`$MPElement[Name=`"SystemCenter!Microsoft.SystemCenter.ManagedComputer`"]$</MonitoringClass>
<RelationshipClass>`$MPElement[Name=`"$($GroupLibraryAlias)!Microsoft.SystemCenter.InstanceGroupContainsEntities`"]$</RelationshipClass>
<IncludeList>
<MonitoringObjectId></MonitoringObjectId>
</IncludeList>
</MembershipRule>
</MembershipRules>"
# Adding the Host GUIDs into the DataSource of the Discovery Rule for the CustomComputerGroupClass
$Discovery_DS_Updated_Configuration = $CustomComputerGroupClassDiscovery_DS.Configuration.Replace("<MonitoringObjectId></MonitoringObjectId>","$HostGUIDs")
# Applying the Host GUIDs into the DataSource of the Discovery Rule for the CustomComputerGroupClass
$CustomComputerGroupClassDiscovery_DS.Configuration = $Discovery_DS_Updated_Configuration
# Adding the DataSource to the Discovery Rule for the CustomComputerGroupClass
$CustomComputerGroupClassDiscovery.DataSource = $CustomComputerGroupClassDiscovery_DS
# Applying changes to the Management Pack in the SCOM Database.
try {
Write-Host "Attempting to Add all changes on SCOM Management Server - [$($ManagementServer)] to Management Pack - [$($MP.DisplayName)]"
$MP.AcceptChanges()
}
catch [System.Exception]
{
echo $_.Exception
exit 2
}
# [---END---] Try-Catch Wrapper for Entire Script.
}
catch [System.Exception]
{
echo $_.Exception
exit 2
}
Write-Host "Deployment of Custom Group Management Pack - [$($ManagementPackDisplayName)] was Successful!"