In the previous tip we illustrated how WMI can ping multiple computers in a very fast way. However, the syntax was awkward. So let’s rewrite the code to make it easier to specify the list of computers you want to ping:
# ping the specified servers with a given timeout (milliseconds)
$ComputerName = 'google.de','microsoft.com','r13-c00'
$TimeoutMillisec = 1000
# convert list of computers into a WMI query string
$query = $ComputerName -join "' or Address='"
Get-WmiObject -Class Win32_PingStatus -Filter "(Address='$query') and timeout=$TimeoutMillisec" | Select-Object -Property Address, StatusCode
Now it’s really easy to ping even large numbers of computers: just add them to $ComputerName which is a string array. You can also use Get-Content to fill $ComputerName, provided you read a file with one computer name per line.
Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.
ReTweet this Tip!