Advertisement
guyrleech

Looping ping with details

Jul 30th, 2024 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.04 KB | Cybersecurity | 0 0
  1. ## run infinite ping test to given address & output details including success/failure & time
  2.  
  3. ## This variant beeps on error
  4.  
  5. $lastFailure=$null;[int]$fails=0;[int]$everySeconds = 30 ;. { while(1){ $started = [datetime]::now ; $r = tnc -Computer 192.168.0.43 -WarningAction SilentlyContinue | select @{n='Date';e={$started}},computername,RemoteAddress,PingSucceeded,@{n='RTT (ms)';e={$_.PingReplyDetails.RoundtripTime}},@{n='Ttl';e={$_.PingReplyDetails.Options.ttl}},@{n='Failures';e={$fails}},@{n='Last Failure';e={$lastfailure}},InterfaceAlias; $r ; if( -not $r.PingSucceeded ) { $fails++ ; $lastfailure = $started ; [console]::Beep( 3000 , 1000 ) } ; if( ($sleep = $everySeconds - ([datetime]::Now - $started).TotalSeconds ) -gt 0 ) { Start-Sleep -Seconds $sleep }}} | ft
  6.  
  7. ## This variant displays a modal dialogue but only 1 at a time & using jobs so loop doesn't hang waiting on dialogue closure
  8.  
  9. $null = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic");$jobby = $null ;$lastFailure=$null;[int]$fails=0;[int]$everySeconds = 30 ;. { while(1){ $started = [datetime]::now ; $r = tnc -Computer 192.168.0.43 -WarningAction SilentlyContinue | select @{n='Date';e={$started}},computername,RemoteAddress,PingSucceeded,@{n='RTT (ms)';e={$_.PingReplyDetails.RoundtripTime}},@{n='Ttl';e={$_.PingReplyDetails.Options.ttl}},@{n='Failures';e={$fails}},@{n='Last Failure';e={$lastfailure}},InterfaceAlias; $r ; if( -not $r.PingSucceeded ) { $fails++ ; $lastfailure = $started ; if( -Not $jobby -or $jobby.State -ine 'running' ) { $jobby | Remove-Job -Force -ea 0 ; $jobby = Start-Job -Scriptblock { [Microsoft.VisualBasic.Interaction]::MsgBox("Ping failed to $($using:r.ComputerName) @ $(($using:r).date.toString('G'))" , 'OKOnly,SystemModal,Exclamation' , 'Ping Failure' )}}} ; if( ($sleep = $everySeconds - ([datetime]::Now - $started).TotalSeconds ) -gt 0 ) { Start-Sleep -Seconds $sleep }}}|ft
  10.  
  11. ## This variant pads the last failure column so that it displays the full date/time (crude but works although output object missing LastFailure)
  12.  
  13. $null = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic");$jobby = $null ;$lastFailure=$null;[int]$fails=0;[int]$everySeconds = 30 ;. { while(1){ $started = [datetime]::now ; $r = tnc -Computer 192.168.0.44 -WarningAction SilentlyContinue | select @{n='Date';e={$started}},computername,RemoteAddress,PingSucceeded,@{n='RTT (ms)';e={$_.PingReplyDetails.RoundtripTime}},@{n='Ttl';e={$_.PingReplyDetails.Options.ttl}},@{n='Failures';e={$fails}},InterfaceAlias; $r ; if( -not $r.PingSucceeded ) { $fails++ ; $lastfailure = $started ; if( -Not $jobby -or $jobby.State -ine 'running' ) { $jobby | Remove-Job -Force -ea 0 ; $jobby = Start-Job -Scriptblock { [Microsoft.VisualBasic.Interaction]::MsgBox("Ping failed to $($using:r.ComputerName) @ $(($using:r).date.toString('G'))" , 'OKOnly,SystemModal,Exclamation' , 'Ping Failure' )}}} ; if( ($sleep = $everySeconds - ([datetime]::Now - $started).TotalSeconds ) -gt 0 ) { Start-Sleep -Seconds $sleep }}}|ft -Property *,@{e={$lastFailure};Label='Last Failure             '}
  14.  
Tags: ping
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement