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?
 
soostibiCommented:
Try this:

Invoke-Command -ComputerName App1 -ScriptBlock {
restart-service service1
restart-service service2
restart-service service3
}
0
 
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
tomd1976Author Commented:
Hello

So the Scriptblock parameter is what I need here? How would it help?
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.