Restarting services sequentially

Hello

I have a Windows 2008 Server named App1.

I would like to remotely restart Service1, Service2 and Service3 on that computer, so was going to run this Powershell command from my admin console:

Invoke-Command -ComputerName App1 {Restart-Service Service1,Service2,Service3}

The only thing is, I need Service1 to stop and start fully before Service2 restarts, which in turn needs to be stop and start completely before Service3 restarts. If, for example, Service1 takes a long time to restart and the script makes Service2 restart before the Service1 restart has completed, then the app will have problems.

Does anyone know how I can do this? Also, at the end of the restarts, I'd like an output showing that the services were all running? Any help appreciated.
tomd1976Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

soostibiCommented:
You can separately stop and then start services. Of course, you have to collect the DependentServices property of each service, so that dependent services could be started too.

Invoke-Command -ComputerName App1 -ScriptBlock {
    Stop-Service service1
    Start-Service service1
    Stop-Service service2
    Start-Service service2
    Stop-Service service3
    Start-Service service3
}
0
soostibiCommented:
And of course you can do a

Get-service service1,service2,service3 -computername App1

command at the end, to see how things are going on.

0
tomd1976Author Commented:
Hello

So the Scriptblock parameter is what I need here? How would it help?
0
Defend Against the Q2 Top Security Threats

Were you aware that overall malware worldwide was down a surprising 42% from Q1'18? Every quarter, the WatchGuard Threat Lab releases an Internet Security Report that analyzes the top threat trends impacting companies worldwide. Learn more by viewing our on-demand webinar today!

soostibiCommented:
Actually you already used the scriptblock parameter, as it is a positional parameter you do not have to write the name of it.
My solution is just shows that you can separate the commands, so the next command starts only if the previous finished. Actually you may do in the scriptblock:

restart-service service1
restart-service service2
restart-service service3

This way the second service restarts only if the first already restarted.
0
tomd1976Author Commented:
Not sure I understand? :-) What do I actually need to type?
0
soostibiCommented:
Try this:

Invoke-Command -ComputerName App1 -ScriptBlock {
restart-service service1
restart-service service2
restart-service service3
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LearnctxEngineerCommented:
This is one I use on one of my text boxes to bring the VMware Server services up and down as I need need. It works by checking whether each services has a dependency on another service or if a service is dependant on it before starting / stopping services.

Start Services:
## Start VMware Services
$arrVMware = Get-Service | ? {$_.DisplayName -Match "VMware"} | Sort-Object DisplayName
foreach ($VM_Service in $arrVMware)
{
	if ($VM_Service.Status -ne "running")
	{
		$VM_ServicesDependedOn = $VM_Service.ServicesDependedOn
		if ($VM_ServicesDependedOn.Count -gt 0)
		{
			Foreach ($Depen in $VM_ServicesDependedOn)
			{
				if ($Depen.Status -ne "running")
				{
					Start-Service $Depen.Name
				}
			}
		}
		Start-Service $VM_Service.Name
    }
}

Open in new window

Stop Services:
## Stop VMware Services
$arrVMware = Get-Service | ? {$_.DisplayName -Match "VMware"} | Sort-Object DisplayName
foreach ($VM_Service in $arrVMware)
{
	if ($VM_Service.Status -eq "running")
	{
		$arrDependentServices = $VM_Service.DependentServices
		if ($arrDependentServices.Count -gt 0)
		{
			Foreach ($Depen in $arrDependentServices)
			{
				if ($Depen.Status -eq "running")
				{
					Stop-Service $Depen.Name
				}
			}
		}
		Stop-Service $VM_Service.Name -force
	}
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.