OpsMgr: Monitoring Windows Services based on StartUp Type & State with a PowerShell-based Monitor

This example uses the PowerShell-based monitor type and wizard to run a PowerShell script that scans for Windows Services configured to start up Automatically but are not running on a agent monitored computer.

For this PowerShell-based unit monitor, the following values were used in General Properties.
Note: This monitor targets the Windows Server Operating System class, and hence WILL BE APPLIED to all its instances if its enabled from the get-go.

image

The PowerShell script used by this monitor to look for Windows Services configured to start up Automatically but are not running on all agent monitored computers has been modified to return its output in a Propertybag.

image

Here is the script used:

This script looks for Windows Services configured to start up Automatically but are not running on all agent monitored computers and not in the exclusion list:

$API = new-object -comObject "MOM.ScriptAPI"
$PropertyBag = $API.CreatePropertyBag()

$flag = 0
$serviceName = ""
$newline = "`r`n"

## Exclusion List to exclude Windows Services from the scan
$exclusionList = @("dddSharedAccess","sppsvc","Server3")

$autoNotRunningServiceList = Get-WmiObject win32_service | where-object{$_.Startmode -eq "Auto" -and $_.State -ne "Running"}

foreach($autoService in $autoNotRunningServiceList)
{
if($autoService.name -notin $exclusionList)
{
$serviceName = $serviceName + $autoService.displayname + ", " + $newline
$flag = $flag + 1 }
}

if( $flag -gt 0)
{    $PropertyBag.AddValue("State","NOTOK")
$PropertyBag.AddValue("Description", "There are " + $flag + " Service(s) with Start Up Type = Automatic but Not Started found: " + $serviceName) }

else
{   $PropertyBag.AddValue("State","OK")
$PropertyBag.AddValue("Description", "No Services with Start Up Type = Automatic but Not Started found.")
}

$PropertyBag

Building expressions based on the value in the Propertybag and mapping monitor conditions to health states were very straight forward as follows. The Expression Builder Pages builds expression that looks for a particular value from the Propertybag that the data source outputs (Property[@Name='State'] ).

image

image

image

The name of the value in the Propertybag was specified in the alert context variable: $Data/Context/Property[@Name='Description'] $

image

Here is an example of the active alert generated and the state change recorded:

image

image

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.