PowerShell/scripts/ping-host.ps1

77 lines
2.3 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2024-03-23 11:57:48 +01:00
.SYNOPSIS
Ping a host
2024-03-23 11:57:48 +01:00
.DESCRIPTION
This PowerShell script pings the given host continously and shows the roundtrip times in a horizontal chart.
.PARAMETER hostname
Specifies the hostname or IP address of the host to ping (windows.com by default)
.PARAMETER timeInterval
Specifies the time interval in milliseconds to repeat the ping (1000 by default)
.EXAMPLE
PS> ./ping-host.ps1
-----------------------------------------
Ping Roundtrip Times to windows.com
-----------------------------------------
#1 ██████████████ 136ms
#2 ████████████████ 154ms
#3 █████████████████████████ 234ms
2024-03-23 11:57:48 +01:00
...
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$hostname = "windows.com", [int]$timeInterval = 1000)
function GetPingLatency([string]$hostname) {
$hostsArray = $hostname.Split(",")
2024-03-24 12:05:31 +01:00
$tasks = $hostsArray | foreach {
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000)
2024-03-23 11:57:48 +01:00
}
2024-03-24 12:05:31 +01:00
[Threading.Tasks.Task]::WaitAll($tasks)
foreach($ping in $tasks.Result) {
if ($ping.Status -eq "Success") { return $ping.RoundtripTime }
}
return 1000
2024-03-23 11:57:48 +01:00
}
function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text)
$num = ($value * 108.0) / $maxValue
2024-03-23 11:57:48 +01:00
while ($num -ge 1.0) {
Write-Host -noNewLine ""
$num -= 1.0
}
if ($num -ge 0.875) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.75) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.625) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.5) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.375) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.25) {
Write-Host -noNewLine ""
} elseif ($num -ge 0.125) {
Write-Host -noNewLine ""
}
Write-Host " $text"
}
try {
2024-10-04 22:02:22 +02:00
Write-Host "`n PING ROUNDTRIP TIMES TO: $hostname" -foregroundColor green
[int]$count = 1
2024-03-23 11:57:48 +01:00
do {
[float]$latency = GetPingLatency $hostname
Write-Host "#$count " -noNewline
2024-03-23 11:57:48 +01:00
WriteChartLine $latency 1000.0 "$($latency)ms"
Start-Sleep -Milliseconds $timeInterval
$count++
2024-03-23 11:57:48 +01:00
} while($true)
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}