One Liners: Restarting Stopped Services
During a recent power “issue”, I had to restart an entire rack full of Hyper-V servers. While an Exchange VM was booting, a networking issue caused the VM to not be able to connect to anything else, including domain controllers. As a result, many services couldn’t start. Rather than bouncing the server, or manually starting the services, this little one liner came in handy. Unfortunately, Get-Service doesn’t expose the startmode. That would make it too easy. So, we use Get-WMIObject:
Get-WMIObject win32_service | Where-Object {$_.name -match "exchange" -and $_.startmode -eq "Auto" -and $_.state -ne "running"} | Start-Service
Of course, we can remove the name check and look for all services on the server that should be (but aren’t) started, and start them:
Get-WMIObject win32_service | Where-Object {$_.startmode -eq "Auto" -and $_.state -ne "running"} | Start-Service
Ståle Hansen has reminded me that in Lync, there is also another solution:
Get-CsWindowsService -ExcludeActivityLevel | Where-Object {$_.Status -like "Stopped"} | Start-CsWindowsService
You should use a WMI Filter instead of Where-Object to be more efficient.
This should also work remotely:
Get-WMIObject win32_service -filter “name like ‘%exchange’% AND startmode=’Auto’ AND state =’Stopped'” -computer EXCH01 | Invoke-WMIMethod -name StartService
Hi, can you do this on a remote machine ??