PowerShell/scripts/check-ping.ps1

48 lines
1.5 KiB
PowerShell
Raw Normal View History

2023-10-31 12:05:32 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-10-28 12:39:58 +02:00
Checks the ping latency
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2024-01-03 11:30:45 +01:00
This PowerShell script measures the ping roundtrip times from the local computer to other ones (10 Internet servers by default).
2021-10-16 16:50:10 +02:00
.PARAMETER hosts
2024-01-03 11:30:45 +01:00
Specifies the hosts to ping, seperated by commata (10 Internet servers by default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-ping.ps1
2024-01-03 11:30:45 +01:00
Online with 18ms latency (13ms...109ms, 0/10 ping loss)
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-03-30 09:06:30 +02:00
#>
2023-08-21 16:53:43 +02:00
param([string]$hosts = "bing.com,cnn.com,dropbox.com,github.com,google.com,ibm.com,live.com,meta.com,x.com,youtube.com")
2021-09-21 20:19:39 +02:00
2021-03-30 09:06:30 +02:00
try {
2023-06-03 08:51:05 +02:00
$hostsArray = $hosts.Split(",")
2023-08-21 16:53:43 +02:00
$parallelTasks = $hostsArray | foreach {
2023-09-25 21:08:20 +02:00
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,750)
2023-06-02 12:41:34 +02:00
}
2023-06-03 08:51:05 +02:00
[int]$min = 9999999
2023-09-25 21:08:20 +02:00
[int]$max = [int]$avg = [int]$success = 0
[int]$total = $hostsArray.Count
2023-08-21 16:53:43 +02:00
[Threading.Tasks.Task]::WaitAll($parallelTasks)
foreach($ping in $parallelTasks.Result) {
2023-09-25 21:08:20 +02:00
if ($ping.Status -ne "Success") { continue }
$success++
[int]$latency = $ping.RoundtripTime
$avg += $latency
if ($latency -lt $min) { $min = $latency }
if ($latency -gt $max) { $max = $latency }
2021-03-30 09:06:30 +02:00
}
2023-09-25 21:08:20 +02:00
[int]$loss = $total - $success
if ($success -ne 0) {
$avg /= $success
2024-01-03 11:30:45 +01:00
Write-Host "✅ Online with $($avg)ms latency ($($min)ms...$($max)ms, $loss/$total ping loss)"
2023-09-12 22:07:26 +02:00
} else {
2023-09-25 21:08:20 +02:00
Write-Host "⚠️ Offline ($loss/$total ping loss)"
2023-08-18 18:27:29 +02:00
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-03-30 09:06:30 +02:00
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-03-30 09:06:30 +02:00
exit 1
2023-06-02 12:29:49 +02:00
}