Jaa


OpsMgr: Using PSGW to display Unhealthy Agents of Instances of a Specific Class

In this post, I would like to demonstrate an example of a PowerShell Grid Widget that can be used to search for and display a list of agents that are either unhealthy or not running by using a simple PowerShell script.

This type of widget is useful as an add-on to dashboards with specific servers or objects being scoped and health state displayed via state widgets etc, for specific teams/groups/products in an organization. This can help the user sidentify issues with the underlying agents of the scoped servers or objects and address them accordingly to ensure continuous and accurate monitoring of these servers or objects from their agents.
    
   
image     
    
Here is an example of a simple PowerShell script used in the PowerShell Grid Widget. In this case the ”Microsoft.Windows.Server.Computer“ class is used to retrieve all managed server objects, and using their display names to search for their corresponding agent object of the "Microsoft.SystemCenter.HealthService" class. If the corresponding agent is either not healthy or not available (stopped), add it to the ScriptContext.ReturnCollection to be displayed on the widget.

    
#////////////////////////////////////////////////////////////////////////////////////////////

$SCOMAgentClass = get-scomclass -Name "Microsoft.SystemCenter.HealthService"

#Class to retrieve all instances of the Windows Server Class
$class = Get-SCOMClass -Name "Microsoft.Windows.Server.Computer"
$Servers = Get-SCOMClassInstance -Class $class

foreach ($OneServer in $Servers)
{

#Note: Make sure name of retrieved object is the same as the display name of its corresponding agent.
$SCOMAgent = Get-SCOMClassInstance -class $SCOMAgentClass | ? {$_.DisplayName -ilike $OneSever.DisplayName+"*"}

#If Agent is either Not Healthy or Not Running
If($SCOMAgent.HealthState -ne "Success" -or $SCOMAgent.IsAvailable -ne $True)
{

$dataObject = $ScriptContext.CreateFromObject($SCOMAgent, "Id=Id,HealthState=HealthState,DisplayName=DisplayName", $null)

#Create Columns
$dataObject["Agent Health State"]=$OneSever.HealthState.ToString()
$dataObject["Is Agent Available"]=$OneSever.IsAvailable.ToString()

$ScriptContext.ReturnCollection.Add($dataObject)
}
}

#////////////////////////////////////////////////////////////////////////////////////////////

Using the sample script above, the PowerShell Grid browser widget should ONLY show objects with agent related issue affecting its overall healthy, and not from other hosted components within the rollup up of the Windows Server object.
     
   
image     
    
    
    

   

Disclaimer:
All information on this blog is provided on an as-is basis with no warranties and for informational purposes only. Use at your own risk. The opinions and views expressed in this blog are those of the author and do not necessarily state or reflect those of my employer.