Query Windows Service Status and Restart with exMon

Created by: Gunnar Steinn Magnússon

In this article, you will read about querying Windows Service status and restart with exMon.

The PowerShell Data Provider in exMon can be used to query Windows Service and make sure they are up and running. In this example, we will query the status of the MapManager within Windows. If the service is stopped for some reason, we will attempt to restart it and send a message to an IT administrator.

Follow these steps to create the query:

  1. Create a Query in exMon Administrator
  2. Choose "PowerShell" as a Data Provider
  3. Enter the following PowerShell script
    $serviceName = '{@servicename[preview:exMon Command Service]}'
    $service = Get-Service -Name $serviceName | select Displayname,Status,@{label="NewStatus";expresssion={$_.Status}}
    if ($service.Status -ne 'Running') {
        #The service is not running. Restart and fetch new status
        Start-Service $serviceName
        $service.NewStatus = (Get-Service -Name $serviceName | select Status).Status
    } else {
        # return empty results
        $exMonResult = New-Object system.Data.DataTable
        $col1 = New-Object system.Data.DataColumn DisplayName,([string])
        $col2 = New-Object system.Data.DataColumn Status,([string])
        $col3 = New-Object system.Data.DataColumn NewStatus,([string])
  4.  Configure email settings and schedule as normally with Extra Parameters:

    --@servicename "SERVICE_NAME"

Example output from the test:

Gunnar is the author of this solution article.