Skip to main content

Query Windows Service Status and Restart with exMon


Christian Hauggaard
Community Manager
Forum|alt.badge.img+5

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";expression={$_.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
    
        $service
    } else {
        # return empty results
        $exMonResult = New-Object system.Data.DataTable
        $col1 = New-Object system.Data.DataColumn DisplayName,([string])
        $exMonResult.columns.add($col1)
        $col2 = New-Object system.Data.DataColumn Status,([string])
        $exMonResult.columns.add($col2)
        $col3 = New-Object system.Data.DataColumn NewStatus,([string])
        $exMonResult.columns.add($col3)
    
        $exMonResult
    
    }
  4.  Configure email settings and schedule as normally with Extra Parameters:
    --@servicename "SERVICE_NAME"

Example output from the test: 

 

Did this topic help you find an answer to your question?

2 replies

  • Starter
  • 1 reply
  • December 30, 2024

Great post, but there is a misspelling in line 4.

expresssion is not with 3 sss, but 2.

 

 


rvgfox
Problem Solver
Forum|alt.badge.img+4
  • Problem Solver
  • 231 replies
  • December 30, 2024

It’s nice, but I suggest in this articles, show the license level needed


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings