In the previous post, we learned we can clear the agents cache, recycle the health service, and this will (hopefully) resolve our gray agent issue. But, what happens when we have to do this for hundreds of agents? One word, PowerShell. PowerShell allows us to automate this task over hundreds of servers to make this very tedious task, actually very quick!Here is the code I use.
Just make sure all of your servers are within the list you are providing, and of course the account you are running as has Local Administrative rights on each server.
$list = gc “.\list.txt”
foreach ($server in $list)
{
Write-Host “$server Check Service: “ -NonewLine
if ((gwmi win32_service -computer $server -filter “name=’HealthService'” | %{$_.State}) -eq “Running”)
{
gwmi win32_service -computer $server -filter “name=’HealthService'” | %{$_.StopService()} | findstr ReturnValue | Tee-Object -var service | out-null
$serviceResult = $service.split(“:”)
if ($serviceResult[1] -eq ” 0″)
{ Write-Host “Successful” -f Green}
else
{Write-Host “Failed” -f Red}
start-sleep 5
$a = gwmi win32_Directory -computer $server -filter “Name=’C:\\PROGRA~1\\SYSTEM~1\\Agent\\HEALTH~1\\HEALTH~1′”
$a.DeleteEx() | findstr ReturnValue | Tee-Object -var status | out-null
$statusresult = $status.split(“:”)
if ($statusResult[1] -eq ” 0″)
{Write-Host “Successful”}
else {Write-Host “Failed”}
gwmi win32_service -computer $server -filter “name=’HealthService'” | %{$_.StartService()} | findstr ReturnValue | Tee-Object -var service | out-null
$serviceResult = $service.split(“:”)
if ($serviceResult[1] -eq ” 0″)
{ Write-Host “Successful” -f Green}
else
{Write-Host “Failed” -f Red}
}
else
{Write-Host Stopped}
}