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
1,850 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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 39

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now