Mike Bishop
asked on
Powershell script to stop service, wait until it's stopped, start the service, wait until it's running then continue
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
do {$svc1.Status}
Until ($svc1.Status -eq $state0)
$svc1 | Set-Service -Status $state1
do {$svc1.Status}
Until ($svc1.Status -eq $state1)
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
do {$svc1.Status}
Until ($svc1.Status -eq $state0)
$svc1 | Set-Service -Status $state1
do {$svc1.Status}
Until ($svc1.Status -eq $state1)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also change the input type from the above default list of server into some members of certain OU with the following changes:
replace the following line
with
replace the following line
$serverlist = get-content "$path\ServerList.txt"
with
$serverlist = Get-QADComputer -SearchRoot 'domain.com/Custom Servers OU' -OSName "Windows*Server*" -ShowProgress -Activity
I just realized I pasted in the wrong thing but it looks like ITStystemEngineer's should work just as well.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the suggestion as well BECraig ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Working through these now and will come back with a response. Thanks for all of the input.
Open in new window