Powershell script to restart HealthService on multiple machines

Below is a handy Powershell script which will restart the Healthservice on multiple servers, simply change the path of serverlist.txt

serverlist.txt is just a txt file with the hostnames of the servers you want to reset the healthservice on.

—————————————————————————————-

$path = “C:\area51\”
$serverlist = get-content “$path\serverlist.txt”
$servicename = “Healthservice”
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”
}
}
else
{
Add-Content “$path\noping.txt” -Value $server
}
}

 

Advertisements
This entry was posted in Misc Stuff, Powershell for SCOM and tagged , , , , , . Bookmark the permalink.

2 Responses to Powershell script to restart HealthService on multiple machines

  1. Boe Prox says:

    Nice stuff! Instead of using sc.exe to remotely restart the service, just call the method for it instead.
    $servicetorestart.Stop()
    $servicetorestart.Start()
    Another benefit of this is that you can call $servicetorestart.Refresh() and it will update the state of the service. I wrote an article dealing with remote services here: http://learn-powershell.net/2012/01/15/startingstopping-and-restarting-remote-services-with-powershell/

  2. scompanion says:

    Thanks for your comments and feedback much appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s