Aug 282014
 

from: PowerShell.com

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:

functionTest-ServiceResponding($ServiceName)
{
  $service=Get-WmiObject-ClassWin32_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.

Leave a Reply