Get Overrides Applied to a Workflow in SCOM 2012 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 nine 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

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 are applied to the workflow that is passed as a parameter to this script. This script supports rules, monitors, discoveries, diagnostics, and recoveries.

To get a list of workflows for each type you can run the following commands:

Get-SCOMRule | select name
Get-SCOMMonitor | select name
Get-SCOMDiscovery | select name
Get-SCOMDiagnostic | select name
Get-SCOMRecovery | select name

Syntax:

.\GetOverrides.ps1 –ManagementServer ‘om01.contoso.com’ –WorkflowID ‘custom.example.test.rule.myrule’

Parameters:

Name Description
ManagementServer Name of MS to connect to
WorkflowID ID of the rule, monitor, discovery, diagnostic, or recovery that you want the available overrides for. You can use the output of the PowerShell cmdlets I listed above. This script supports only a single workflow name at this time.

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     $WorkflowID
  6     )
  7 
  8 Write-Host "Version 1.0"
  9 Write-Host "ManagementServer:"$ManagementServer
 10 Write-Host "WorkflowID:"$WorkflowID
 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)  
 54 
 55   $criteria = [string]::Format("Name = '{0}'", $wfID)
 56   $wfTypes = ('recovery','diagnostic','recovery','monitor','rule','discovery')
 57   $bFound = $false
 58 
 59   foreach ($wfType in $wfTypes)
 60   {
 61     if ($bFound) {break}
 62     switch ($wfType)
 63     {
 64       'discovery' { $workflow = GetDiscovery -mg $mg -criteria $criteria; if ($workflow -ne $null) {$bFound = $true; break} }
 65       'rule' { $workflow = GetRule -mg $mg -criteria $criteria; if ($workflow -ne $null) {$bFound = $true; break} }
 66       'monitor' { $workflow = GetMonitor -mg $mg -criteria $criteria; if ($workflow -ne $null) {$bFound = $true; break} }
 67       'recovery' { $workflow = GetRecovery -mg $mg -criteria $criteria; if ($workflow -ne $null) {$bFound = $true; break} }
 68       'diagnostic' { $workflow = GetDiagnostic -mg $mg -criteria $criteria; if ($workflow -ne $null) {$bFound = $true; break} }
 69     }
 70   }
 71 
 72   if ($workflow -eq $null)
 73   {
 74     Write-Host "Unable to Find Workflow, Exiting"$wfID -ForegroundColor Red
 75     Write-Host $_.Exception.Message -ForegroundColor Yellow
 76     exit  
 77   }
 78 
 79   return $workflow
 80 }
 81 
 82 function GetWorkflowOverrides
 83 {
 84   param($guid, $overrides)
 85 
 86   $wOverrides = New-Object System.Collections.ArrayList
 87 
 88   foreach ($override in $overrides)
 89   {
 90     $property = GetOverrideType -override $override
 91   
 92     $oGUID = ($override.$property.ToString()).Split("=")[1]
 93 
 94     if ($guid -eq $oGUID) 
 95     {
 96       [void]$wOverrides.Add($override)
 97     }
 98   }
 99 
100   return $wOverrides
101 }
102 
103 function GetInstance
104 {
105   param($mg, $guid)
106 
107   $instance = $mg.GetMonitoringObject($guid)
108   $instance = $instance.DisplayName + "|" + $instance.Path
109 
110   return $instance
111 }
112 
113 function GetClass
114 {
115   param($mg, $id)
116 
117   $class = $mg.GetMonitoringClass($id)
118 
119   return $class
120 }
121 
122 function GetDiscovery
123 {
124   param($mg, $criteria)
125   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringDiscoveryCriteria($criteria)
126   $discovery = $mg.GetMonitoringDiscoveries($searchCriteria)[0]
127   return $discovery
128 }
129 
130 function GetRule
131 {
132   param($mg, $criteria)
133   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringRuleCriteria($criteria)
134   $rule = $mg.GetMonitoringRules($searchCriteria)[0]
135   return $rule
136 }
137 
138 function GetMonitor
139 {
140   param($mg, $criteria)
141   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitorCriteria($criteria)
142   $monitor = $mg.GetMonitors($searchCriteria)[0]
143   return $monitor
144 }
145 
146 function GetDiagnostic
147 {
148   param($mg, $criteria)
149   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringDiagnosticCriteria($criteria)
150   $diagnostic = $mg.GetMonitoringDiagnostics($searchCriteria)[0]
151   return $diagnostic
152 }
153 
154 function GetRecovery
155 {
156   param($mg, $criteria)
157   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringRecoveryCriteria($criteria)
158   $recovery = $mg.GetMonitoringRecoveries($searchCriteria)[0]
159   return $recovery
160 }
161 
162 function GetAllOverrides
163 {
164   param($mg)
165   $criteria = ([string]::Format("Name like '%'"))
166   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringOverrideCriteria($criteria)
167   $overrides = $MG.GetMonitoringOverrides($searchCriteria)
168   return $overrides
169 }
170 
171 function GetOverrideType
172 {
173   param($override)
174 
175   switch ($override.XmlTag.ToString())
176   {
177     'DiscoveryPropertyOverride' { $property = 'Discovery' }
178     'DiscoveryConfigurationOverride' { $property = 'Discovery' }
179     'RulePropertyOverride' { $property = 'Rule' }
180     'RuleConfigurationOverride' { $property = 'Rule' }
181     'MonitorPropertyOverride' { $property = 'Monitor' }
182     'MonitorConfigurationOverride' { $property = 'Monitor' }
183     'DiagnosticPropertyOverride' { $property = 'Diagnostic' }
184     'DiagnosticConfigurationOverride' { $property = 'Diagnostic' }
185     'RecoveryPropertyOverride' { $property = 'Recovery' }
186     'RecoveryConfigurationOverride' { $property = 'Recovery' }
187     'SecureReferenceOverride' { $property = 'SecureReference' }
188     'CategoryOverride' { $property = 'Category' }
189   }
190 
191   return $property
192 }
193 
194 function GetFormattedOverrides
195 {
196   param($mg, $overrides, $workflow)
197 
198   $formattedOverrides = New-Object System.Collections.ArrayList
199 
200   foreach ($override in $overrides)
201   {
202     $class = GetClass -mg $MG -id $override.Context.ToString().Split("=")[1]
203     $property = GetOverrideType -override $override
204     if ($override.ContextInstance) {$iValue = GetInstance -mg $mg -guid $override.ContextInstance} else {$iValue = 'Not Applicable'}
205     if ($override.ContextInstance) {$gValue = $override.ContextInstance} else {$gValue = 'Not Applicable'}
206     if ($override.Property) {$pValue = $override.Property} else {$pValue = 'Not Applicable'}
207     if ($override.Module) {$mValue = $override.Module} else {$mValue = 'Not Applicable'}
208     if ($override.Parameter) {$paValue = $override.Parameter} else {$paValue = 'Not Applicable'}
209 
210     $o = New-Object PSObject -Property @{
211       OverrideID = $override.Name
212       OverrideMP = $override.GetManagementPack().Name
213       OverrideMPDisplayName = $override.GetManagementPack().DisplayName
214       OverrideType = $override.XmlTag
215       OverrideProperty = $pValue
216       OverrideModule = $mValue
217       OverrideParameter = $paValue
218       OverrideValue = $override.Value
219       WorkflowID = $workflow.Name
220       WorkflowName = $workflow.DisplayName
221       WorkflowMP = $workflow.GetManagementPack().Name
222       WorkflowMPDisplayName = $workflow.GetManagementPack().DisplayName
223       WorkflowType = $property
224       ContextID = $class.Name
225       ContextName = $class.DisplayName
226       ContextMP = $class.GetManagementPack().Name
227       ContextMPDisplayName = $class.GetManagementPack().DisplayName
228       ContextInstanceGUID = $gValue
229       ContextInstanceNameAndPath = $iValue
230       Enforced = $override.Enforced
231     }
232     [void]$formattedOverrides.Add($o)
233   }
234 
235   return $formattedOverrides
236 }
237 
238 #Connect to SCOM Management Group
239 $MG = GetSCOMManagementGroup -ms $ManagementServer
240 
241 #Get workflow
242 $Workflow = GetWorkflow -mg $MG -wfID $WorkflowID
243 
244 #Get all overrides in management group
245 $Overrides = GetAllOverrides -mg $MG
246 
247 #Get all overrides applied to the workflow
248 $WorkflowOverrides = GetWorkflowOverrides -guid $Workflow.Id -overrides $Overrides
249 #$WorkflowOverrides
250 
251 #Get formatted overrides
252 $WorkflowOverridesFormatted = GetFormattedOverrides -mg $MG -overrides $WorkflowOverrides -workflow $Workflow
253 
254 #Print Output
255 $WorkflowOverridesFormatted | Format-List -Property OverrideID, OverrideMP, OverrideMPDisplayName, OverrideType, OverrideProperty, OverrideModule, OverrideParameter, OverrideValue, WorkflowID, WorkflowMP, WorkflowMPDisplayName, WorkflowType, ContextID, ContextName, ContextMP, ContextMPDisplayName, ContextInstanceGUID, ContextInstanceNameAndPath, Enforced

GetOverrides.renametops1