Hi how i can make a fast ping of servers in my Active Directory?
Easy with powershell
Each morning i have to check if all my servers are ok. So it start with a basic ping to see if it ‘s online or not. When you have hundreds of servers a script is needed.
So i’ve decide to check every server in my Active Directory. Not all servers are mine but with a filter it’ll do the job!
Ok, let’s see the script now, first of all i need a basic ping function. With Powershell 2.0 we can use the Test-Conneciton command
function PingResult { param($item) $Result = Test-Connection $item -Count 1 -BufferSize 16 -Quiet if($Result -match 'True'){Write-Host -ForegroundColor Green $item} Else {Write-Host -ForegroundColor Red $item} }
Now we need to list all servers in my AD, it can be done with the dsquery computer command. The “-limit 0 ” command must be used if you have more than 100 result to display. The “-O rdn” is used to have a usefull output of the dsquery command.
Write-Host "Checking Citrix Servers" $Citrix = dsquery computer "ou=xxx,ou=xxx,ou=xx,DC=DoTheJob,dc=local" -limit 0 -O rdn
In my case i don’t want to check all servers, i’m only checking Citrix and Vmware servers. Each of them have been named with a particular convention name. So i need filter my servers list.
if($item -match 'CX')
Now i have my server list and i can ping them, but there’s a last thing to do! When you use dsquery the output will add quotes for example you’ll have as a servername: “SRVCX-01”. You must delete quote before ping so add this to your script:
$item = $item -replace '"'
Now all it’s ok, you can ping all your servers!
The whole script:
Clear-Host function PingResult { param($item) $Result = Test-Connection $item -Count 1 -BufferSize 16 -Quiet if($Result -match 'True'){Write-Host -ForegroundColor Green $item} Else {Write-Host -ForegroundColor Red $item} } Write-Host "Checking Citrix Servers" $Citrix = dsquery computer "ou=xxx,ou=sss,ou=xxx,DC=DoTheJob,dc=local" -limit 0 -O rdn foreach($item in $Citrix) { if($item -match 'CX') { $item = $item -replace '"' PingResult $item } }
Hope it could help someone!