PowerShell/Scripts/check-ping.ps1

43 lines
1.2 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-21 20:19:39 +02:00
check-ping.ps1 [<hosts>]
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2021-09-24 17:19:49 +02:00
Checks the ping latency from the local computer to selected Internet hosts
2021-09-21 21:02:15 +02:00
(default is: 'amazon.com,apple.com,bing.com,cnn.com,dropbox.com,facebook.com,google.com,live.com,twitter.com,youtube.com)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-ping
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-29 17:50:03 +02:00
Author: Markus Fleschutz · License: CC0
2021-03-30 09:06:30 +02:00
#>
2021-09-21 21:02:15 +02:00
param([string]$hosts = "amazon.com,apple.com,bing.com,cnn.com,dropbox.com,facebook.com,google.com,live.com,twitter.com,youtube.com")
2021-09-21 20:19:39 +02:00
2021-03-30 09:06:30 +02:00
try {
2021-09-21 20:19:39 +02:00
write-progress "Sending pings to $hosts..."
$HostsArray = $hosts.Split(",")
$Pings = test-connection -count 1 -computerName $HostsArray
2021-03-30 09:06:30 +02:00
[int]$Min = 9999999
[int]$Max = 0
[int]$Avg = 0
foreach($Ping in $Pings) {
2021-03-30 09:43:58 +02:00
if ($IsLinux) {
[int]$Latency = $Ping.latency
} else {
2021-03-30 09:47:21 +02:00
[int]$Latency = $Ping.ResponseTime
2021-03-30 09:43:58 +02:00
}
if ($Latency -lt $Min) { $Min = $Latency }
if ($Latency -gt $Max) { $Max = $Latency }
$Avg += $Latency
2021-03-30 09:06:30 +02:00
}
$Avg = $Avg / $Pings.count
2021-09-21 21:02:15 +02:00
"✔️ $Avg ms net latency average ($Min ms min, $Max ms max, $($Pings.count) hosts)"
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-30 09:06:30 +02:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-03-30 09:06:30 +02:00
exit 1
}