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,302 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
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 7

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 7

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 7

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

830 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