Last active
April 29, 2020 15:40
-
-
Save mwisnicki/2195ab2501fda338ad228ccab432ba22 to your computer and use it in GitHub Desktop.
Basic ping monitor in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$lastDate = $null | |
$diff = $null | |
cat .\Ping-Monitor.log | select-string "timeout" | %{ | |
switch -regex ($_) { | |
'(?<date>.*) timeout' { | |
$date = [DateTime]::Parse($Matches.date) | |
if ($lastDate) { | |
$diff = $date - $lastDate | |
} | |
"$_ $diff" | |
$lastDate = $date | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start-transcript -append Ping-Monitor.log | |
ping google.com -t | %{ | |
$datestr = get-date -format s | |
switch -regex ($_) { | |
'Reply from .* bytes=.* time=(?<time>.*)ms TTL=.*' { if ($Matches.time -as [int] -gt 30) { "$datestr $($Matches.time)" } } | |
'timed out' { "$datestr timeout" } | |
} | |
} | |
stop-transcript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$lastDate = $null | |
$diff = $null | |
ping google.com -t | %{ | |
$date = get-date | |
switch -regex ($_) { | |
'timed out' { | |
if ($lastDate) { | |
$diff = $date - $lastDate | |
} | |
echo "$(get-date -date $date -format s) timeout $diff" | |
$lastDate = $date | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment