Get All Overrides in a SCOM 2012 Management Group using PowerShell

This is a continuation of a Data Center Automation series of posts that I have been working on with Anders Bengtsson. Here are the first 10 posts in this series:

Creating Management Packs in SCOM 2012 with PowerShell
Creating Performance Collection Rules in SCOM 2012 with PowerShell
Creating Event Based Alerting Rules in SCOM 2012 with PowerShell
Enabling or Disabling Workflows in SCOM 2012 with PowerShell
Deleting Workflows in SCOM 2012 with PowerShell
Creating Groups in SCOM 2012 with PowerShell
Adding References to MPs in SCOM 2012 with PowerShell
Modifying Explicit Group Membership in SCOM 2012 with PowerShell
Get Parameters that can be Overridden in SCOM 2012 with PowerShell
Get Overrides Applied to a Workflow in SCOM 2012 using PowerShell

As of this post this script is not included as an activity in the Operations Manager Admin Integration Pack but will be in the next version.

The purpose of this script is to get all overrides that exist in a management group and write the output to a csv file.

Syntax:

.\GetAllOverrides.ps1 –ManagementServer ‘om01.contoso.com’ –OutputDirectory ‘c:\temp’

Parameters:

Name Description
ManagementServer Name of MS to connect to
OutputDirectory Directory to write CSV file containing all the overrides

Output:

Name Description
OverrideID The id of the override
OverrideMP The management pack that the override exists in
OverrideMPDisplayName The friendly name of the management pack that the override exists in
OverrideType The type of the override found (discovery, rule, etc…)
OverrideProperty The property that the override applies to. This only applies to the hard coded properties specific to the type of workflow passed.
OverrideModule If the parameter that the override applies to is a configuration parameter then this is the unique name of the module that the override is defined
OverrideParameter If the parameter that the override applies to is a configuration parameter then this is the name of the parameter.
OverrideValue The property or configuration value of the override
WorkflowID The id of the rule, discovery, monitor, etc… that the override applies to
WorkflowName The friendly name of the rule, discovery, monitor, etc… that the override applies to
WorkflowMP The management pack that the workflow, in which the override applies to, exists in
WorkflowMPDisplayName The friendly name that the management pack that the rule, monitor, recovery, etc… that the override applies to exists in
WorkflowType The type of the workflow that the override applies to (discovery, diagnostic, etc…)
ContextID The id of the class / group that the override is applied to
ContextName The friendly name of the class / group that the override is applied to
ContextMP The management pack that the class / group, in which the override is applied to, exists
ContextMPDisplayName The friendly name of the management pack that the class / group, in which the override is applied to, exists
ContextInstanceGUID If the override is also applied to an instance of a class then this is the GUID of that instance.
ContextInstanceNameAndPath If the override is also applied to an instance of a class then this is the DisplayName and Path of that instance
Enforced Whether or not the override has the Enforced property set
   1 Param(            
  2     [parameter(Mandatory=$true)]            
  3     $ManagementServer,
  4     [parameter(Mandatory=$true)]
  5     $OutputDirectory
  6     )
  7 
  8 Write-Host "Version 1.0"
  9 Write-Host "ManagementServer:"$ManagementServer
 10 Write-Host "OutputDirectory:"$OutputDirectory
 11 
 12 function GetSCOMManagementGroup
 13 {
 14   param($ms)
 15   try
 16   {
 17     $mg = New-Object Microsoft.EnterpriseManagement.ManagementGroup($ms)
 18   }
 19   catch
 20   {
 21     Write-Host "Failed to Connect to SDK, Exiting:"$ms -ForegroundColor Red
 22     Write-Host $_.Exception.Message -ForegroundColor Yellow
 23     exit
 24   }
 25   return $mg
 26 }
 27 
 28 function GetWorkflowFromOverride
 29 {
 30   param($mg, $override)
 31 
 32   switch ($override.XmlTag.ToString())
 33   {
 34     'DiscoveryPropertyOverride' { $property = 'Discovery' }
 35     'DiscoveryConfigurationOverride' { $property = 'Discovery' }
 36     'RulePropertyOverride' { $property = 'Rule' }
 37     'RuleConfigurationOverride' { $property = 'Rule' }
 38     'MonitorPropertyOverride' { $property = 'Monitor' }
 39     'MonitorConfigurationOverride' { $property = 'Monitor' }
 40     'DiagnosticPropertyOverride' { $property = 'Diagnostic' }
 41     'DiagnosticConfigurationOverride' { $property = 'Diagnostic' }
 42     'RecoveryPropertyOverride' { $property = 'Recovery' }
 43     'RecoveryConfigurationOverride' { $property = 'Recovery' }
 44     'SecureReferenceOverride' { $property = 'SecureReference' }
 45     'CategoryOverride' { $property = 'Category' }
 46   }
 47 
 48   return $workflow
 49 }
 50 
 51 function GetWorkflow
 52 {
 53   param($mg, $wfID, $property)  
 54 
 55   switch ($property.ToLower())
 56   {
 57     'discovery' { $workflow = GetDiscovery -mg $mg -guid $wfID }
 58     'rule' { $workflow = GetRule -mg $mg -guid $wfID }
 59     'monitor' { $workflow = GetMonitor -mg $mg -guid $wfID }
 60     'recovery' { $workflow = GetRecovery -mg $mg -guid $wfID }
 61     'diagnostic' { $workflow = GetDiagnostic -mg $mg -guid $wfID }
 62     'securereference' { $workflow = GetSecureReference -mg $mg -guid $wfID }
 63   }
 64 
 65   return $workflow
 66 }
 67 
 68 function GetWorkflowOverrides
 69 {
 70   param($guid, $overrides)
 71 
 72   $wOverrides = New-Object System.Collections.ArrayList
 73 
 74   foreach ($override in $overrides)
 75   {
 76     $property = GetOverrideType -override $override
 77   
 78     $oGUID = ($override.$property.ToString()).Split("=")[1]
 79 
 80     if ($guid -eq $oGUID) 
 81     {
 82       [void]$wOverrides.Add($override)
 83     }
 84   }
 85 
 86   return $wOverrides
 87 }
 88 
 89 function GetInstance
 90 {
 91   param($mg, $guid)
 92 
 93   $instance = $mg.GetMonitoringObject($guid)
 94   $instance = $instance.DisplayName + "|" + $instance.Path
 95 
 96   return $instance
 97 }
 98 
 99 function GetClass
100 {
101   param($mg, $id)
102 
103   $class = $mg.GetMonitoringClass($id)
104   return $class
105 }
106 
107 function GetDiscovery
108 {
109   param($mg, $guid)
110   $discovery = $mg.GetMonitoringDiscovery($guid)
111   return $discovery
112 }
113 
114 function GetRule
115 {
116   param($mg, $guid)
117   $rule = $mg.GetMonitoringRule($guid)
118   return $rule
119 }
120 
121 function GetMonitor
122 {
123   param($mg, $guid)
124   $monitor = $mg.GetMonitor($guid)
125   return $monitor
126 }
127 
128 function GetDiagnostic
129 {
130   param($mg, $guid)
131   $diagnostic = $mg.GetMonitoringDiagnostic($guid)
132   return $diagnostic
133 }
134 
135 function GetRecovery
136 {
137   param($mg, $guid)
138   $recovery = $mg.GetMonitoringRecovery($guid)
139   return $recovery
140 }
141 
142 function GetSecureReference
143 {
144   param($mg, $guid)
145   $secureReference = $mg.GetMonitoringSecureReference($guid)
146   return $secureReference
147 }
148 
149 function GetAllOverrides
150 {
151   param($mg)
152   $criteria = ([string]::Format("Name like '%'"))
153   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringOverrideCriteria($criteria)
154   $overrides = $MG.GetMonitoringOverrides($searchCriteria)
155   return $overrides
156 }
157 
158 function GetOverrideType
159 {
160   param($override)
161 
162   switch ($override.XmlTag.ToString())
163   {
164     'DiscoveryPropertyOverride' { $property = 'Discovery' }
165     'DiscoveryConfigurationOverride' { $property = 'Discovery' }
166     'RulePropertyOverride' { $property = 'Rule' }
167     'RuleConfigurationOverride' { $property = 'Rule' }
168     'MonitorPropertyOverride' { $property = 'Monitor' }
169     'MonitorConfigurationOverride' { $property = 'Monitor' }
170     'DiagnosticPropertyOverride' { $property = 'Diagnostic' }
171     'DiagnosticConfigurationOverride' { $property = 'Diagnostic' }
172     'RecoveryPropertyOverride' { $property = 'Recovery' }
173     'RecoveryConfigurationOverride' { $property = 'Recovery' }
174     'SecureReferenceOverride' { $property = 'SecureReference' }
175     'CategoryOverride' { $property = 'Category' }
176   }
177 
178   return $property
179 }
180 
181 function GetFormattedOverrides
182 {
183   param($mg, $overrides)
184 
185   $formattedOverrides = New-Object System.Collections.ArrayList
186 
187   foreach ($override in $overrides)
188   {
189     $property = GetOverrideType -override $override
190     try
191     {
192       $class = GetClass -mg $MG -id $override.Context.ToString().Split("=")[1]
193       $contextID = $class.Name
194       $contextName = $class.DisplayName
195       $contextMP = $class.GetManagementPack().Name
196       $contextMPDisplayName = $class.GetManagementPack().DisplayName
197     }
198     catch
199     {
200       $contextID = 'Not Found'
201       $contextName = 'Not Found'
202       $contextMP = 'Not Found'
203       $contextMPDisplayName = 'Not Found'
204     }
205 
206     if (!($property -eq 'Category'))
207     {
208       try 
209       {
210         $workflow = GetWorkflow -mg $MG -wfID $override.$property.ToString().Split("=")[1] -property $property
211         $workflowID = $workflow.Name
212         $workflowName = $workflow.DisplayName
213         $workflowMP = $workflow.GetManagementPack().Name
214         $workflowMPDisplayName = $workflow.GetManagementPack().DisplayName
215       } 
216       catch 
217       {
218         $workflowID = 'Not Found'
219         $workflowName = 'Not Found'
220         $workflowMP = 'Not Found'
221         $workflowMPDisplayName = 'Not Found'
222       }
223     }
224     else
225     {
226       $workflowID = $override.$property
227       $workflowName = 'Not Applicable'
228       $workflowMP = 'Not Applicable'
229       $workflowMPDisplayName = 'Not Applicable'
230     }
231 
232     if ($override.ContextInstance)
233     {
234       $gValue = $override.ContextInstance
235       try
236       {
237         $iValue = GetInstance -mg $mg -guid $override.ContextInstance
238       }
239       catch
240       {
241         $iValue = 'Not Found'
242       }
243     } 
244     else 
245     {
246       $iValue = 'Not Applicable'
247       $gValue = 'Not Applicable'
248     }
249 
250     if ($override.Property) {$pValue = $override.Property} else {$pValue = 'Not Applicable'}
251     if ($override.Module) {$mValue = $override.Module} else {$mValue = 'Not Applicable'}
252     if ($override.Parameter) {$paValue = $override.Parameter} else {$paValue = 'Not Applicable'}
253 
254     $o = New-Object PSObject -Property @{
255       OverrideID = $override.Name
256       OverrideMP = $override.GetManagementPack().Name
257       OverrideMPDisplayName = $override.GetManagementPack().DisplayName
258       OverrideType = $override.XmlTag
259       OverrideProperty = $pValue
260       OverrideModule = $mValue
261       OverrideParameter = $paValue
262       OverrideValue = $override.Value
263       WorkflowID = $workflowID
264       WorkflowName = $workflowName
265       WorkflowMP = $workflowMP
266       WorkflowMPDisplayName = $workflowMPDisplayName
267       WorkflowType = $property
268       ContextID = $contextID
269       ContextName = $contextName
270       ContextMP = $contextMP
271       ContextMPDisplayName = $contextMPDisplayName
272       ContextInstanceGUID = $gValue
273       ContextInstanceNameAndPath = $iValue
274       Enforced = $override.Enforced
275     }
276     [void]$formattedOverrides.Add($o)
277   }
278 
279   return $formattedOverrides
280 }
281 
282 #Connect to SCOM Management Group
283 $MG = GetSCOMManagementGroup -ms $ManagementServer
284 
285 #Get all overrides in management group
286 $Overrides = GetAllOverrides -mg $MG
287 
288 #Get formatted overrides
289 $OverridesFormatted = GetFormattedOverrides -mg $MG -overrides $Overrides
290 
291 #Print Output
292 $OutputPath = $OutputDirectory + '\' + $MG.Name + 'Overrides.csv'
293 $OverridesFormatted | Sort-Object OverrideMP | Select-Object OverrideID, OverrideMP, OverrideMPDisplayName, OverrideType, OverrideProperty, OverrideModule, OverrideParameter, OverrideValue, WorkflowID, WorkflowMP, WorkflowMPDisplayName, WorkflowType, ContextID, ContextName, ContextMP, ContextMPDisplayName, ContextInstanceGUID, ContextInstanceNameAndPath, Enforced | Export-Csv -Path $OutputPath -NoTypeInformation
294 
295 Write-Host "Output Location:"$OutputPath
296 Write-Host "Script Complete"

GetAllOverrides.renametops1