Skip to content

Instantly share code, notes, and snippets.

@mwisnicki
Last active April 29, 2020 15:40
Show Gist options
  • Save mwisnicki/2195ab2501fda338ad228ccab432ba22 to your computer and use it in GitHub Desktop.
Save mwisnicki/2195ab2501fda338ad228ccab432ba22 to your computer and use it in GitHub Desktop.
Basic ping monitor in PowerShell
$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
}
}
}
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
$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