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
3,307 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

626 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