Techibee.com

how to ping computers using powershell

I have written a small function for myself which I am using in all my scripts to check the availability of computer before acting on it. Here it is…

function ping-host {
param($computer)
$status = Get-WmiObject -Class Win32_PingStatus -Filter “Address=’$computer'”
if( $status.statuscode -eq 0) {
    return 1
}
else {
    return 0
 }
}

So, basically what this does is, returns 1 when the given computer is reachable or “0” if it is not. Below is a small example to demonstrate the functionality.

if (ping-host mycomp) {

write-host “reachable”

}

else {

write-host “unreachable”

}

Let me know if you know any other simple way…

Exit mobile version