Get Parameters that can be Overridden in SCOM 2012 with 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 eight 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

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.

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:

.\GetOveriddableParameters.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.
   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 GetManagementPack
 29 {
 30   param ($mg, $mpID)
 31   try
 32   {
 33     $mp = $mg.GetManagementPacks($mpID)[0]
 34   }
 35   catch
 36   {
 37     Write-Host "Management Pack Not Found, Exiting:"$mpID -ForegroundColor Red
 38     Write-Host $_.Exception.Message -ForegroundColor Yellow
 39     exit
 40   }
 41   return $mp
 42 }
 43 
 44 function GetMPID
 45 {
 46   param($mg, $wfID)  
 47   $criteria = [string]::Format("Name = '{0}'", $wfID)
 48 
 49   $wfTypes = ('recovery','diagnostic','recovery','monitor','rule','class', 'discovery')
 50 
 51   foreach ($wfType in $wfTypes)
 52   {
 53     switch ($wfType)
 54     {
 55       'discovery' { try {$mpID = GetDiscoveryFromMG -mg $mg -criteria $criteria} catch {}}
 56       'class' { try {$mpID = (GetClassMPFromMG -mg $mg -criteria $criteria).Name} catch {}}
 57       'rule' { try {$mpID = GetRuleFromMG -mg $mg -criteria $criteria} catch {}}
 58       'monitor' { try {$mpID = GetMonitorFromMG -mg $mg -criteria $criteria} catch {}}
 59       'recovery' { try {$mpID = GetRecoveryFromMG -mg $mg -criteria $criteria} catch {}}
 60       'diagnostic' { try {$mpID = GetDiagnosticFromMG -mg $mg -criteria $criteria} catch {}}
 61     }
 62   }
 63 
 64   if ($mpID -eq $null)
 65   {
 66     Write-Host "Unable to Find MP, Exiting"$mpID -ForegroundColor Red
 67     Write-Host $_.Exception.Message -ForegroundColor Yellow
 68     exit  
 69   }
 70 
 71   return $mpID
 72 }
 73 
 74 function GetWorkflowType
 75 {
 76   param($mg, $mpID, $wfID)
 77 
 78   $mp = GetManagementPack -mg $mg -mpID $mpID
 79   $workflow = $mp.FindManagementPackElementByName($wfID)
 80   $wfType = $workflow.GetType()
 81   
 82   switch($wfType)
 83   {
 84     'Microsoft.EnterpriseManagement.Configuration.ManagementPackDiscovery' {$workflowType = 'discovery'}
 85     'Microsoft.EnterpriseManagement.Configuration.ManagementPackRule' {$workflowType = 'rule'}
 86     'Microsoft.EnterpriseManagement.Configuration.ManagementPackUnitMonitor' {$workflowType = 'monitor'}
 87     'Microsoft.EnterpriseManagement.Configuration.ManagementPackAggregateMonitor' {$workflowType = 'monitor'}
 88     'Microsoft.EnterpriseManagement.Configuration.ManagementPackDependencyMonitor' {$workflowType = 'monitor'}
 89     'Microsoft.EnterpriseManagement.Configuration.ManagementPackDiagnostic' {$workflowType = 'diagnostic'}
 90     'Microsoft.EnterpriseManagement.Configuration.ManagementPackRecovery' {$workflowType = 'recovery'}
 91     default 
 92     {
 93       Write-Host "Unable to Find Workflow:"$wfID -ForegroundColor Red
 94       Write-Host "Workflow Type:"$wfType -ForegroundColor Red
 95       Write-Host "Exiting" -ForegroundColor Red
 96       exit
 97     }
 98   }
 99 
100   return $workflowType
101 }
102 
103 function GetDiscovery
104 {
105   param ($mp, $discoveryID)
106   $discovery = $mp.GetDiscovery($discoveryID)
107   return $discovery
108 }
109 
110 function GetRule
111 {
112   param ($mp, $ruleID)
113   $rule = $mp.GetRule($ruleID)
114   return $rule
115 }
116 
117 function GetMonitor
118 {
119   param ($mp, $monitorID)
120   $monitor = $mp.GetMonitor($monitorID)
121   return $monitor
122 }
123 
124 function GetRecovery
125 {
126   param ($mp, $recoveryID)
127   $recovery = $mp.GetRecovery($recoveryID)
128   return $recovery
129 }
130 
131 function GetDiagnostic
132 {
133   param ($mp, $diagnosticID)
134   $diagnostic = $mp.GetDiagnostic($diagnosticID)
135   return $diagnostic
136 }
137 
138 function GetDiscoveryFromMG
139 {
140   param($mg, $criteria)
141   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringDiscoveryCriteria($criteria)
142   $discovery = $mg.GetMonitoringDiscoveries($searchCriteria)[0]
143   $mp = $discovery.GetManagementPack()
144   return $mp.Name
145 }
146 
147 function GetClassFromMG
148 {
149   param($mg, $criteria)
150   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria($criteria)
151   $class = $mg.GetMonitoringClasses($searchCriteria)[0]
152   return $class
153 }
154 
155 function GetRuleFromMG
156 {
157   param($mg, $criteria)
158   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringRuleCriteria($criteria)
159   $rule = $mg.GetMonitoringRules($searchCriteria)[0]
160   $mp = $rule.GetManagementPack()
161   return $mp.Name
162 }
163 
164 function GetMonitorFromMG
165 {
166   param($mg, $criteria)
167   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitorCriteria($criteria)
168   $monitor = $mg.GetMonitors($searchCriteria)[0]
169   $mp = $monitor.GetManagementPack()
170   return $mp.Name
171 }
172 
173 function GetDiagnosticFromMG
174 {
175   param($mg, $criteria)
176   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringDiagnosticCriteria($criteria)
177   $diagnostic = $mg.GetMonitoringDiagnostics($searchCriteria)[0]
178   $mp = $diagnostic.GetManagementPack()
179   return $mp.Name
180 }
181 
182 function GetRecoveryFromMG
183 {
184   param($mg, $criteria)
185   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringRecoveryCriteria($criteria)
186   $recovery = $mg.GetMonitoringRecoveries($searchCriteria)[0]
187   $mp = $recovery.GetManagementPack()
188   return $mp.Name
189 }
190 
191 function GetClassMPFromMG
192 {
193   param($mg, $criteria)
194   $searchCriteria = new-object Microsoft.EnterpriseManagement.Configuration.MonitoringClassCriteria($criteria)
195   $class = $mg.GetMonitoringClasses($searchCriteria)[0]
196   $mp = $class.GetManagementPack()
197   return $mp
198 }
199 
200 function GetWorkflow
201 {
202   param($mg, $mpID, $id, $type)
203 
204   $mp = GetManagementPack -mg $mg -mpID $mpID
205 
206   try
207   {
208     switch($type)
209     {
210       'discovery' {$workflow = GetDiscovery -mp $mp -discoveryID $id}
211       'rule' {$workflow = GetRule -mp $mp -ruleID $id}
212       'monitor' {$workflow = GetMonitor -mp $mp -monitorID $id}
213       'recovery' {$workflow = GetRecovery -mp $mp -recoveryID $id}
214       'diagnostic' {$workflow = GetDiagnostic -mp $mp -diagnosticID $id}
215     }
216   }
217   catch
218   {
219     Write-Host "Workflow Not Found, Exiting:"$id -ForegroundColor Red
220     Write-Host $_.Exception.Message -ForegroundColor Yellow
221     exit
222   }
223   return $workflow
224 }
225 
226 function GetOverridableParameters
227 {
228   param($workflow, $workflowType)
229 
230   $parameters = New-Object System.Collections.ArrayList
231 
232   #Property Overrides
233   switch ($workflowType)
234   {
235     'discovery' {$pValues = [enum]::GetNames([Microsoft.EnterpriseManagement.Configuration.ManagementPackWorkflowProperty])}
236     'rule' {$pValues = [enum]::GetNames([Microsoft.EnterpriseManagement.Configuration.ManagementPackWorkflowProperty])}
237     'monitor' {$pValues = [enum]::GetNames([Microsoft.EnterpriseManagement.Configuration.ManagementPackMonitorProperty])}
238     'recovery' {$pValues = [enum]::GetNames([Microsoft.EnterpriseManagement.Configuration.ManagementPackWorkflowProperty])}
239     'diagnostic' {$pValues = [enum]::GetNames([Microsoft.EnterpriseManagement.Configuration.ManagementPackWorkflowProperty])}
240   }
241 
242   foreach ($o in $pValues)
243   {
244     $param = New-Object PSObject -Property @{
245       Name = $o
246       DisplayName = $o
247       WorkflowType = $workflowType
248     }
249     [void]$parameters.Add($param)
250   }
251 
252   #Configuration Overrides
253   switch ($workflowType)
254   {
255     'discovery' { $cValues = $workflow.GetOverrideableParametersByModule() }
256     'rule' { $cValues = $workflow.GetOverrideableParametersByModule() }
257     'monitor' { try {$cValues = $workflow.GetOverrideableParameters()} catch {} }
258     'recovery' { $cValues = $workflow.GetOverrideableParametersByModule() }
259     'diagnostic' { $cValues = $workflow.GetOverrideableParametersByModule() }
260   }
261 
262   foreach ($module in $cValues.Keys)
263   {
264     foreach ($parameter in $cValues.$module)
265     {      
266       $param = New-Object PSObject -Property @{
267       Name = $parameter.Name
268       DisplayName = $parameter.DisplayName
269       WorkflowType = $workflowType
270       ModuleName = $module.Name
271       ParameterType = $parameter.ParameterType
272       }
273       [void]$parameters.Add($param)
274     }
275   }
276 
277   return $parameters
278 }
279 
280 #Adding SCOM Module
281 try { Import-Module OperationsManager } catch
282 { 
283   Write-Host "SCOM Module Not Found, Exiting" -ForegroundColor Red
284   Write-Host $_.Exception.Message -ForegroundColor Yellow
285   exit
286 }
287 
288 #Connect to SCOM Management Group
289 $MG = GetSCOMManagementGroup -ms $ManagementServer
290 
291 #Get Workflow Management Pack ID from Management Group
292 $WorkflowMPID = GetMPID -mg $MG -wfID $WorkflowID
293 
294 #Get Workflow Type
295 $WorkflowType = GetWorkflowType -mg $MG -mpID $WorkflowMPID -wfID $WorkflowID
296 
297 #Get Workflow
298 $WorkFlow = GetWorkflow -mg $mg -mpID $WorkflowMPID -id $WorkflowID -type $WorkflowType
299 
300 #Get Overridable Parameters
301 $Parameters = GetOverridableParameters -workflow $Workflow -workflowType $WorkflowType
302 
303 #Print Output
304 $Parameters | Format-Table Name, DisplayName, WorkflowType, ModuleName, ParameterType

GetOverridableParameters.renametops1