Saturday, 4 April 2009

Get-ServiceStatus

The Need:
This week I needed to check whether a particular service (ersvc) had stopped on a number of computers. I pulled all the windows 2000 and XP computers from AD into a text file (C:\Comps.txt) with the Get-QADComputer cmdlet, then pinged each computer first. If the computer was up the script ran the check on the service, if the computer was down it ignored it.

The only output I got was computers that were up that had the service installed but was stopped.



The Script:
$comps = (Get-Content C:\Comps.txt)
function Ping-Host
{$ping = gwmi -q "SELECT * FROM Win32_Pingstatus WHERE Address = '$comp'"
if($ping.statusCode -eq 0)
{$ersvc = gwmi win32_service -comp $comp | where { $_.name -eq "ersvc"}
if ($ersvc.state -eq "stopped")
{Write-Host PC: $comp Service: $ersvc.name State: $ersvc.state}}}
foreach ($comp in ($comps))
{ping-host}

No comments: