SCOM: How to get Health state of a monitor with PowerShell.

Recently I got a request to generate a Critical Alert only when the state of a specific monitor on all servers in a Group was different than healthy. It’s possible to create PowerShell based monitors in SCOM (more on that topic later on..)

Anyway, here is my piece of code to get the monitor state:

Import-Module OperationsManager
#Specify SCOM Group
$Group = Get-SCOMGroup -DisplayName "TEST GROUP"
$Members = $Group.GetRelatedMonitoringObjects()
foreach ($member in $Members)
{
#get the server object.
$alertobject = Get-SCOMMonitoringobject -Name $member
#get the monitor, in this example DisplayName is used as parameter.
$alertmonitor = Get-SCOMMonitor -DisplayName "Run As Account Verification"
 #If the server is not healthy, available and the monitor is enabled then..
 If (($alertobject) -and ($alertobject.HealthState -ne "Success" -and $alertobject.IsAvailable -eq "True") -and ($alertmonitor.enabled -eq "True")) {
 #HealthState from OperationalStateCollection and only return the first line.   
 $alertmonitor.OperationalStateCollection | select HealthState -First 1
 }
}

Leave a Reply

Your email address will not be published.