Powershell Wait for service to be stopped or started

后端 未结 4 892
暗喜
暗喜 2021-01-31 19:36

I have searched both this forum and through google and can\'t find what I need. I have a quite large script and I\'m looking for some code that will check if the service is star

4条回答
  •  青春惊慌失措
    2021-01-31 20:36

    The following will loop and verify the status of the given services until the number of services with the "Running" state is equal to zero (hence they are stopped), so you can use this if you are waiting for services to Stop.

    I've added a $MaxRepeat variable, which will prevent this from running for ever. It will run 20 times max as defined.

    $services = "Service Bus *"
    $maxRepeat = 20
    $status = "Running" # change to Stopped if you want to wait for services to start
    
    do 
    {
        $count = (Get-Service $services | ? {$_.status -eq $status}).count
        $maxRepeat--
        sleep -Milliseconds 600
    } until ($count -eq 0 -or $maxRepeat -eq 0)
    

提交回复
热议问题