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,580 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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
I just realized I pasted in the wrong thing but it looks like ITStystemEngineer's should work just as well.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Assisted Solution

by:becraig
becraig earned 166 total points
Comment Utility
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
Comment Utility
Thanks for the suggestion as well BECraig ;-)
0
 
LVL 39

Accepted Solution

by:
footech earned 167 total points
Comment Utility
@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
Comment Utility
Working through these now and will come back with a response.  Thanks for all of the input.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 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

7 Experts available now in Live!

Get 1:1 Help Now