Wednesday, March 25, 2015

Retrieve monitors and rules on SCOM Agent

For SCOM admins, there is always a need to know what a servers has been monitored for.
There is a powershell that is provided by 2 MVPs previously (Stefan Stranger and Jeremy Pavleck) which serves this purpose.

To use run from OpsMgr Command Shell:
Get-OpsMgrWorkflows_v1.ps1 -agentname "myagent.contoso.com" | export-csv -path c:\temp\workflows.csv

Save the below as Get-OpsMgrWorkflows_v1.ps1
        
#param ([string]$agentname = $(read-host "Please enter OpsMgr Agent Name"))             
            
function Get-AgentWorkflow($agentname)            
{            
 #Original Script from Jeremy Pavleck.            
 #http://www.pavleck.net/2008/06/sp1-gem-finding-rules-running-on-remote-agents/            
 #Use the OpsMgr Task Show Running Rules and Monitors.            
 $taskobj = Get-Task | Where-Object {$_.Name -eq "Microsoft.SystemCenter.GetAllRunningWorkflows"}            
             
 # Grab HealthService class object            
 $hsobj = Get-MonitoringClass -name "Microsoft.SystemCenter.HealthService"            
 # Find HealthService object defined for named server            
 $monobj = Get-MonitoringObject -MonitoringClass $hsobj | Where-Object {$_.DisplayName -match $agentname}            
             
 #Start Task GetAllRunningWorkflows            
 $taskOut = Start-Task -Task $taskobj -TargetMonitoringObject $monobj            
 [xml]$taskXML = $taskOut.OutPut             
             
 #Get Workflows            
 $workflows=$taskXML.selectnodes("/DataItem/Details/Instance/Workflow")            
             
 #Retrieve Monitors            
 $monitors = get-monitor            
             
 #Retrieve Rules            
 $rules = get-rule            
             
 #Retrieve Discoveries"            
 #Used the Group-object because there are some discovery rules with the same DisplayName            
 $discoveries = get-discovery | select-object -Unique            
             
 #Get Overrides"            
 #monitoroverrides = foreach ($monitor in Get-ManagementPack | get-override | where {$_.monitor}) {get-monitor | where {$_.Id -eq $monitor.monitor.id}}            
 #$rulesoverrides = foreach ($rule in Get-ManagementPack | get-override | where {$_.rule}) {get-rule | where {$_.Id -eq $rule.rule.id}}            
 #$discoveryoverrides = foreach ($discovery in Get-ManagementPack | get-override | where {$_.discovery}) {get-discovery | where {$_.Id -eq $discovery.discovery.id}}            
            
            
 #Check for each workflow if it's a Rule or Monitor or Discovery.            
 foreach ($workflow in $workflows)            
 {            
  #Check for Monitor            
  $monitor = $monitors | where-object {$_.Name -eq $workflow."#text"}            
              
  if ($monitor -eq $null)            
  {            
   #Check for Rule            
   $rule = $rules | where-object {$_.Name -eq $workflow."#text"}            
   if ($rule -eq $null)            
   {             
    #Check for Discovery            
    $discovery = $discoveries | where-object {$_.Name -eq $workflow."#text"}            
    if ($discovery -eq $null)            
    {            
                
    }            
    else            
    {            
     #Get ManagementPack            
     $mp = $discovery.getmanagementpack()            
     #Check if Discovery has an override            
     #$flag = $discoveryoverrides | Where-Object {$_.DisplayName -eq $discovery.DisplayName}            
     #if ($flag -eq $null)            
     #{            
     # $override = "false"            
     #}            
     #else            
     #{            
     # $override = "true"            
     #}            
     $discobject = new-object System.Management.Automation.PSObject            
     $discobject = $discobject | add-member -membertype NoteProperty -name Type -value "Discovery" -passthru            
     $discobject = $discobject | add-member -membertype NoteProperty -name DisplayName -value $discovery.DisplayName -passthru            
     $discobject = $discobject | add-member -membertype NoteProperty -name Description -value $discovery.Description -passthru            
     #$discobject = $discobject | add-member -membertype NoteProperty -name Override -value $override -passthru            
     $discobject = $discobject | add-member -membertype NoteProperty -name ManagementPack -value $mp.DisplayName -passthru            
     $discobject            
    }            
   }            
   else            
   {            
    $mp = $rule.getmanagementpack()            
    #Check if Rule has an override            
    #$flag = $ruleoverrides | Where-Object {$_.DisplayName -eq $rule.DisplayName}            
    #if ($flag -eq $null)            
    #{            
    # $override = "false"            
    #}            
    #else            
    #{            
    # $override = "true"            
    #}            
    $ruleobject = new-object System.Management.Automation.PSObject            
    $ruleobject = $ruleobject | add-member -membertype NoteProperty -name Type -value "Rule" -passthru            
    $ruleobject = $ruleobject | add-member -membertype NoteProperty -name DisplayName -value $rule.DisplayName -passthru            
    $ruleobject = $ruleobject | add-member -membertype NoteProperty -name Description -value $rule.Description -passthru            
    #$ruleobject = $ruleobject | add-member -membertype NoteProperty -name Override -value $override -passthru            
    $ruleobject = $ruleobject | add-member -membertype NoteProperty -name ManagementPack -value $mp.DisplayName -passthru            
    $ruleobject            
   }            
  }            
  else            
  {            
   #Get ManagementPack for Monitor            
   $mp = $monitor.getmanagementpack()            
   #Check if Monitor has an override            
   #$flag = $monitoroverrides | Where-Object {$_.DisplayName -eq $monitor.DisplayName}            
   #if ($flag -eq $null)            
   #{            
   # $override = "false"            
   #}            
   #else            
   #{            
   # $override = "true"            
   #}            
   $monitorobject = new-object System.Management.Automation.PSObject            
   $monitorobject = $monitorobject | add-member -membertype NoteProperty -name Type -value "Monitor" -passthru            
   $monitorobject = $monitorobject | add-member -membertype NoteProperty -name DisplayName -value $monitor.DisplayName -passthru            
   $monitorobject = $monitorobject | add-member -membertype NoteProperty -name Description -value $monitor.Description -passthru            
   #$monitorobject = $monitorobject | add-member -membertype NoteProperty -name Override -value $override -passthru            
   $monitorobject = $monitorobject | add-member -membertype NoteProperty -name ManagementPack -value $mp.DisplayName -passthru            
   $monitorobject            
  }            
 }            
            
            
}            
            
Get-AgentWorkflow $agentname

No comments:

Post a Comment