Test Service Responsiveness

by Aug 27, 2014

All PowerShell Versions

To test whether a particular service is still responding, use a clever trick. First, ask WMI for the service you want to check. WMI will happily return the process ID of the underlying process.

Next, look up this process, and the process object will tell you whether the process is frozen or responding:

function Test-ServiceResponding($ServiceName)
{
  $service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'"
  $processID = $service.processID
  
  $process = Get-Process -Id $processID
  
  $process.Responding
}

This example would check whether the Spooler service is still responding:

 
PS> Test-ServiceResponding -ServiceName Spooler
True
 

Note that the example code assumes that the service is running. If you wanted to, you could add a check to exclude non-running services yourself.

Twitter This Tip! ReTweet this Tip!