9

#PSTip Wait for a Service to reach a specified status

So, in your script, you need to wait for a service until it reaches a specified status and performs an action based on the new state. One way to achieve this, and an ineffective one, would be to poll the status using a while loop:

$svc = Get-Service W3SVC
while($svc.State -ne 'Stopped')
{
	Start-Sleep -Seconds 1
}

... do your thing here...

Instead, you could wait for the service to reach the specified status using one of its native methods:

$svc.WaitForStatus('Stopped')

This will wait infinitely for the service to reach the specified state, and script execution is halted until the service state changes. Waiting forever for the service to change its state may not be what we want to do, so instead we can use the second overload of the WaitForStatus method and specify an expiration time-out value.

# wait for 5 seconds
$svc.WaitForStatus('Stopped','00:00:05')

... the rest of the script ...
Filed in: Columns, Tips and Tricks Tags: , ,

9 Responses to "#PSTip Wait for a Service to reach a specified status"

  1. pamarths says:

    Hi, Small correction to above examples. The ineffective method has some typos. There is no parameter called “state” for the object $SVC. It should be changed to “Status”. And inside while loop, a extra statement should added, $svc.refresh() so that it gets the latest status of service each time it is entering while loop.

    • ShayLevy says:

      Thanks for the feedback @pamarths. I used the term ‘state’ to describe the current configuration of the service, not for referring to its ‘Status’ property. I agree it might be confusing.

  2. DVS says:

    What’s wrong with polling the status using a while loop, other than more lines of code? Checking out the WaitForStatus on MSDN is says “The WaitForStatus method waits approximately 250 milliseconds between each status check.” So to me is sounds like it’s just doing some looping too.

  3. Igor says:

    How do i make it wait more than 30 seconds?

    i run this method and it times out at 30 seconds. so the wiat for status never really runs.

    $service.Stop();

    $service.WaitForStatus(‘Stopped’)

  4. Chris K. says:

    Except in practice WaitForStatus doesn’t work on an object. You can try it for yourself. Once you create an object, that object is static and does not reflect status changes unless you use the Refresh method. I don’t know why they didn’t build Refresh into WaitForStatus but as it stands, it’s a worthless method for using on an object. The only way you can use WaitForStatus would be like:
    (Get-Service “service”).WaitForStatus(‘stopped’,’00:00:05′)

    • David Lant says:

      PowerShell 4.0: It does seem to work for objects to me.

      $svc = Get-Service w3svc
      Stop-Service w3svc
      # Still shows as Running
      $svc.Status
      #Returns instantly because the status has been refreshed
      $svc.WaitForStatus(“Stopped”,”00:10:00″)
      # Now shows the Stopped status
      $svc.Status

      It seems like only the Status method doesn’t issue a Refresh, while the WaitForStatus does. Or am I missing something?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: