Powershell script to stop service, wait until it's stopped, start the service, wait until it's running then continue
Posted on 2014-11-04
I'm new to Powershell scripting and am trying to write a script that perform the following steps:
1. Stop the service on a remote computer (Service is VWServicePE, Computer is Computer1 in this example)
2. Wait for the service to stop before continuing
3. Continue once it's confirmed to be stopped
4. Start the service on the remote computer
5. Wait for the service to start before continuing
6. Continue once it's confirmed to be running
What I have below works to stop and start the service but the looping is not right as far as it waiting for the service to stop/start before proceeding. Can anyone offer recommendations?
$state0 = "Stopped"
$state1 = "Running"
$svc1 = Get-Service -Name VWServicesPE -ComputerName Computer1
$svc1 | Set-Service -Status $state0
Until ($svc1.Status -eq $state0)
$svc1 | Set-Service -Status $state1
Until ($svc1.Status -eq $state1)