PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

image rotation

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.


As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Test Service Responsiveness

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!

Copyright 2012 PowerShell.com. All rights reserved.