Solved

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
8
2,747 Views
Last Modified: 2014-12-11
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)
0
Comment
Question by:dmbishop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40422783
This might work, I think you could go about this better though.

$state0 = "Stopped"
$state1 = "Running"
$svc1 = Get-Service -Name WSService -ComputerName Computer1
$svc1 | Set-Service -Status $state0
while($svc1.Status -ne  $state0) { $svc1.Status }

Open in new window

0
 
LVL 8

Assisted Solution

by:Senior IT System Engineer
Senior IT System Engineer earned 167 total points
ID: 40422786
try this one man:

$path = "C:\tmp\"
$serverlist = get-content "$path\ServerList.txt"
$servicename = "<Display name of service>"

foreach ($server in $serverlist){
	$result = Test-Connection $server -Count 1 -Quiet
	if ($result -eq "True") {
		$servicetorestart = Get-Service -name $servicename -ComputerName $server -ErrorAction SilentlyContinue
		if ($servicetorestart.Name -ne $servicename) {
			Add-Content "$path\noservice.txt" -Value ("$server" + " " + "Service $servicename is not present")
		}
		else {
			#sc.exe \\$server stop "$servicename"
			#Start-Sleep -Seconds 5
			#sc.exe \\$server start "$servicename"
			Add-Content "$path\ServiceRestarted.txt" -Value $server
		}
	}
	else {
		Add-Content "$path\noping.txt" -Value $server
	}
}

Open in new window

0
 
LVL 8

Expert Comment

by:Senior IT System Engineer
ID: 40422797
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
$serverlist = get-content "$path\ServerList.txt"

with
$serverlist = Get-QADComputer -SearchRoot 'domain.com/Custom Servers OU' -OSName "Windows*Server*" -ShowProgress -Activity
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 29

Expert Comment

by:becraig
ID: 40422829
I just realized I pasted in the wrong thing but it looks like ITStystemEngineer's should work just as well.
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 166 total points
ID: 40422875
Here would be my suggestion:

$service = "Service" 
$svc1 = Get-Service $service -computername computer
Stop-Service $service
do { $svcstate = get-service $service | select -expa status; $svcstate } Until ($svcstate -ne "Running")

Open in new window

0
 
LVL 8

Expert Comment

by:Senior IT System Engineer
ID: 40422895
Thanks for the suggestion as well BECraig ;-)
0
 
LVL 40

Accepted Solution

by:
footech earned 167 total points
ID: 40423090
@dmbishop - Just by way of explanation, the reason yours didn't work was because you set with
$svc1 = Get-Service -Name VWServicesPE -ComputerName Computer1
$svc is a snapshot of what was returned by that command.  The contents of that variable don't change along with the state of the service.  So, you have to query the status of the service each time through the loop.  I would also add a Start-Sleep command so you're not trying to check it many times per second.
0
 

Author Comment

by:dmbishop
ID: 40424204
Working through these now and will come back with a response.  Thanks for all of the input.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question