Ping + %time% with PowerShell

A simple ping is enough to know if a computer is up or not, but when we are keeping track of the status of a computer or just doing a planned operation and need to know the out of service times, we need to know at what exact time the computer was down.
We can easily get the information with a bit of powershell:

$erroractionpreference="SilentlyContinue"
if ($args[0] -eq $null){$strComputer=read-host "Computer?"}
else{$strComputer=$args[0]}
#-------variables to change---------
$seconds=4
$logfile=".\pingtime.$strComputer.log"
#-----------------------------------
$msg="------- Execting PingTime every $seconds seconds -------"
write-host $msg -fore cyan
out-file $logfile -input $msg -append
 do
 {
 $time=get-date -uformat "%d/%m/%Y %H:%M:%S"
 $result = Test-Connection -ComputerName $strComputer -Count 1 -BufferSize 16
 if ($? -eq $true){$color="white";$status="Success ($($result.responsetime) ms)"}
 else{$color="darkgray";$status="TimedOut"}
 $msg="$time Ping $($strComputer.toupper()) $status"
 write-host $msg -fore $color
 if ($previouscolor -ne $color){$previouscolor=$color;out-file $logfile -input $msg -append}
 start-sleep -s $seconds
 }
 while ($a -eq $null) #INFINITE LOOP

The cmdlet test-connection has a parameter -quiet that give us just a $true or $false. In this case I didn't use it because I wanted the response time of the ping ($result.responsetime)

Comments