PowerShell/docs/check-ping.md

94 lines
2.9 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*check-ping.ps1*
================
2021-11-08 21:36:42 +01:00
2023-09-01 17:53:03 +02:00
This PowerShell script measures the ping roundtrip times from the local computer to other computers (10 Internet servers by default).
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./check-ping.ps1 [[-hosts] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
-hosts <String>
2023-09-01 17:53:03 +02:00
Specifies the hosts to check, seperated by commata (default is: amazon.com,bing.com,cnn.com,dropbox.com,github.com,google.com,live.com,meta.com,x.com,youtube.com)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
2023-09-01 17:53:03 +02:00
Default value bing.com,cnn.com,dropbox.com,github.com,google.com,ibm.com,live.com,meta.com,x.com,youtube.com
2021-11-08 21:36:42 +01:00
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-08-06 21:36:33 +02:00
PS> ./check-ping.ps1
2023-09-13 09:49:05 +02:00
✅ Online with 18ms latency average (13ms...109ms, 0/10 ping loss)
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Checks the ping latency
.DESCRIPTION
2023-09-01 17:53:03 +02:00
This PowerShell script measures the ping roundtrip times from the local computer to other computers (10 Internet servers by default).
2022-11-17 20:02:26 +01:00
.PARAMETER hosts
2023-09-01 17:53:03 +02:00
Specifies the hosts to check, seperated by commata (default is: amazon.com,bing.com,cnn.com,dropbox.com,github.com,google.com,live.com,meta.com,x.com,youtube.com)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-ping.ps1
2023-09-13 09:49:05 +02:00
✅ Online with 18ms latency average (13ms...109ms, 0/10 ping loss)
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-09-01 17:53:03 +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")
2022-11-17 20:02:26 +01:00
try {
2023-07-29 09:45:37 +02:00
$hostsArray = $hosts.Split(",")
2023-09-01 17:53:03 +02:00
$parallelTasks = $hostsArray | foreach {
2023-10-19 08:12:00 +02:00
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,750)
2022-11-17 20:02:26 +01:00
}
2023-07-29 09:45:37 +02:00
[int]$min = 9999999
2023-10-19 08:12:00 +02:00
[int]$max = [int]$avg = [int]$success = 0
[int]$total = $hostsArray.Count
2023-09-01 17:53:03 +02:00
[Threading.Tasks.Task]::WaitAll($parallelTasks)
foreach($ping in $parallelTasks.Result) {
2023-10-19 08:12:00 +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 }
2023-07-29 09:45:37 +02:00
}
2023-10-19 08:12:00 +02:00
[int]$loss = $total - $success
if ($success -ne 0) {
$avg /= $success
Write-Host "✅ Online with $($avg)ms latency average ($($min)ms...$($max)ms, $loss/$total ping loss)"
2023-09-13 09:49:05 +02:00
} else {
2023-10-19 08:12:00 +02:00
Write-Host "⚠️ Offline ($loss/$total ping loss)"
2023-09-01 17:53:03 +02:00
}
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2023-12-16 10:12:58 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-ping.ps1 as of 12/16/2023 10:12:19)*