-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddToMP-CustomServiceMonitor_v1.0.ps1
193 lines (149 loc) · 8.85 KB
/
AddToMP-CustomServiceMonitor_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
# --- [AddToMP-CustomServiceMonitor_v1.0] PowerShell Script ---
#
# Author(s): Ryan Irujo
# Inception: 09.02.2013
# Last Modified: 09.05.2013
#
# Description: This Script provides an automated method of adding Custom Service Monitors in SCOM to an existing
# unsealed Management Pack.
#
#
# Changes: 09.02.2013 - [R. Irujo]
# - Modified the GetUnitMonitorTypes Query when creating a new Service Monitor to use the
# ManagementPackUnitMonitorTypeCritiera with a String Query to improve the performance
# of the script. This change will be applied to earlier related scripts.
#
# 09.03.2013 - [R. Irujo]
# - Changed the AlertMessage variable to generate a unique GUID for itself while being declared. This was necessary
# to ensure that any additional monitors added later on we're not forced to use the same Alert Message settings.
# - Additional Notes added into Script.
# - Cleaned up MP Query section to no longer require a ForEach loop to parse the results for the Custom Class.
#
# 09.05.2013 - [R. Irujo]
# - Added a check to see if the Service Monitor to be added already exists in the Management Pack.
# - Removed Try-Catch Block from section that determines if the Management Pack already exists.
#
#
# Additional Notes: Mind the BACKTICKS throughout the Script! In particular, any XML changes that you may decide to add/remove/change
# will require use of them to escape special characters that are commonly used.
#
#
# Syntax: ./AddToMP-CustomServiceMonitor_v1.0 <Management_Server> <Management_Pack_Display_Name> <Service_Name> <Service_Display_Name> <Check_Startup_Type_Value>
#
# Example: ./AddToMP-CustomServiceMonitor_v1.0 SCOMMS01.fabrikam.local "Custom Service Monitors - Main MP" wuauserv "Windows Update" True
param($ManagementServer,$ManagementPackDisplayName,$ServiceName,$ServiceDisplayName,$CheckStartupType)
# [---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 (!$ManagementPackDisplayName) {
Write-Host "A Management Pack Display Name must be provided, i.e. - Custom Service Monitor MP01."
exit 2;
}
if (!$ServiceName) {
Write-Host "The Name of the Service you want to Monitor must be provided, i.e. - wuauserv."
exit 2;
}
if (!$ServiceDisplayName) {
Write-Host "The Display Name of the Service you want to Monitor must be provided, i.e. - Windows Update."
exit 2;
}
if (!$CheckStartupType) {
Write-Host "A Check Startup Type Value for the Service Monitor must be provided, i.e. 'True' or 'False'."
exit 2;
}
Write-Host "Management Server: $($ManagementServer)"
Write-Host "Management Pack Display Name: $($ManagementPackDisplayName)"
Write-Host "Service Name: $($ServiceName)"
Write-Host "Service Display Name: $($ServiceDisplayName)"
Write-Host "Check Startup Type [True or False]: $($CheckStartupType)`n"
Write-Host "Connecting to the SCOM Management Group"
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ManagementServer)
# 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. Script will now exit."
exit 2;
}
Write-Host "Management Pack - [$($ManagementPackDisplayName)] was found in SCOM. Script will continue."
# Determining if the Service Monitor already exists in the Management Pack based upon its Name.
$ServiceMonitorCheck = $MP.GetMonitors()
Foreach ($Item in $ServiceMonitorCheck) {
If ($Item.DisplayName -eq $ServiceDisplayName) {
Write-Host "[$($ServiceDisplayName)] - Service Monitor already exists in [$($ManagementPackDisplayName)]. Script will now exit."
exit 2;
}
}
Write-Host "[$($ServiceDisplayName)] - Service Monitor was not found in [$($ManagementPackDisplayName)]. Script will continue."
# Retrieving the Custom Class in the Management Pack.
$CustomClass = $MP.GetClasses()[0]
# Creating New Service Monitor
$MonitorTypeQuery = "Name = 'Microsoft.Windows.CheckNTServiceStateMonitorType'"
$MonitorCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackUnitMonitorTypeCriteria($MonitorTypeQuery)
$ServiceMonitorType = $MG.GetUnitMonitorTypes($MonitorCriteria)[0]
$Monitor = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackUnitMonitor($MP,($ServiceName.ToString().Replace("$","_")+"_"+[Guid]::NewGuid().ToString().Replace("-","")),"Public")
# Setting new New Monitor Up as a Service Monitor and targeting the Hosts of the Group in the MP.
$Monitor.set_DisplayName($ServiceDisplayName)
$Monitor.set_Category("Custom")
$Monitor.set_TypeID($ServiceMonitorType)
$Monitor.set_Target($CustomClass)
# Configure Monitor Alert Settings
$Monitor.AlertSettings = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorAlertSettings
$Monitor.AlertSettings.set_AlertOnState("Error")
$Monitor.AlertSettings.set_AutoResolve($true)
$Monitor.AlertSettings.set_AlertPriority("Normal")
$Monitor.AlertSettings.set_AlertSeverity("Error")
$Monitor.AlertSettings.set_AlertParameter1("`$Target/Host/Property[Type=`"Windows!Microsoft.Windows.Computer`"]/NetworkName$")
$Monitor.AlertSettings.AlertMessage
# Configure Alert Settings - Alert Message
$AlertMessage = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackStringResource($MP, ("CustomServiceMonitorAlertMessage_"+$ServiceName.ToString().Replace("$","_")+"_"+[Guid]::NewGuid().ToString().Replace("-","")))
$AlertMessage.set_DisplayName("The $($ServiceDisplayName) Service has Stopped")
$AlertMessage.set_Description("The $($ServiceDisplayName) Service has Stopped on {0}")
$Monitor.AlertSettings.set_AlertMessage($AlertMessage)
# Configure Health States for the Monitor
$HealthyState = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackUnitMonitorOperationalState($Monitor, "Success")
$ErrorState = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackUnitMonitorOperationalState($Monitor, "Error")
$HealthyState.set_HealthState("Success")
$HealthyState.set_MonitorTypeStateID("Running")
$ErrorState.set_HealthState("Error")
$ErrorState.set_MonitorTypeStateID("NotRunning")
$Monitor.OperationalStateCollection.Add($HealthyState)
$Monitor.OperationalStateCollection.Add($ErrorState)
# Specifying Service Monitoring Configuration
$MonitorConfig = "<ComputerName>`$Target/Host/Property[Type=`"Windows!Microsoft.Windows.Computer`"]/NetworkName$</ComputerName>
<ServiceName>$($ServiceName)</ServiceName>
<CheckStartupType>$($CheckStartupType)</CheckStartupType>"
$Monitor.set_Configuration($MonitorConfig)
# Specify Parent Monitor by ID
$MonitorCriteria = New-Object Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorCriteria("Name='System.Health.AvailabilityState'")
$ParentMonitor = $MG.GetMonitors($MonitorCriteria)[0]
$Monitor.ParentMonitorID = [Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference``1[Microsoft.EnterpriseManagement.Configuration.ManagementPackAggregateMonitor]]::op_implicit($ParentMonitor)
# Applying changes to the Management Pack in the SCOM Database.
try {
Write-Host "Attempting to Add [$($Monitor.DisplayName)] - Service Monitor 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 Service Monitor for [$($ServiceDisplayName)] to Management Pack - [$($ManagementPackDisplayName)] was Successful!"