mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-08 17:28:18 +02:00
Improved ping-host.ps1
This commit is contained in:
parent
d72ffb9490
commit
e09c53bcc5
@ -1,75 +1,40 @@
|
|||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
Ping a host
|
Pings a host
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
This PowerShell script pings the given host continously and shows the roundtrip times in a horizontal chart.
|
This PowerShell script pings the given host.
|
||||||
.PARAMETER hostname
|
.PARAMETER hostname
|
||||||
Specifies the hostname or IP address of the host to ping (windows.com by default)
|
Specifies the hostname or IP address to ping (windows.com by default)
|
||||||
.PARAMETER timeInterval
|
|
||||||
Specifies the time interval in milliseconds to repeat the ping (1000 by default)
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
PS> ./ping-host.ps1
|
PS> ./ping-host.ps1 x.com
|
||||||
-----------------------------------------
|
✅ x.com is up with 10ms latency.
|
||||||
Ping Roundtrip Times to windows.com
|
|
||||||
-----------------------------------------
|
|
||||||
#1 ██████████████ 136ms
|
|
||||||
#2 ████████████████ 154ms
|
|
||||||
#3 █████████████████████████ 234ms
|
|
||||||
...
|
|
||||||
.LINK
|
.LINK
|
||||||
https://github.com/fleschutz/PowerShell
|
https://github.com/fleschutz/PowerShell
|
||||||
.NOTES
|
.NOTES
|
||||||
Author: Markus Fleschutz | License: CC0
|
Author: Markus Fleschutz | License: CC0
|
||||||
#>
|
#>
|
||||||
|
|
||||||
param([string]$hostname = "windows.com", [int]$timeInterval = 1000)
|
param([string]$hostname = "windows.com")
|
||||||
|
|
||||||
function GetPingLatency([string]$hostname) {
|
function GetPingLatency([string]$hostname) {
|
||||||
$hostsArray = $hostname.Split(",")
|
$hostsArray = $hostname.Split(",")
|
||||||
$tasks = $hostsArray | foreach {
|
$tasks = $hostsArray | foreach {
|
||||||
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1000)
|
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,1500)
|
||||||
}
|
}
|
||||||
[Threading.Tasks.Task]::WaitAll($tasks)
|
[Threading.Tasks.Task]::WaitAll($tasks)
|
||||||
foreach($ping in $tasks.Result) {
|
foreach($ping in $tasks.Result) {
|
||||||
if ($ping.Status -eq "Success") { return $ping.RoundtripTime }
|
if ($ping.Status -eq "Success") { return $ping.RoundtripTime }
|
||||||
}
|
}
|
||||||
return 1000
|
return 1500
|
||||||
}
|
|
||||||
|
|
||||||
function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text)
|
|
||||||
$num = ($value * 108.0) / $maxValue
|
|
||||||
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 {
|
try {
|
||||||
Write-Host "`n PING ROUNDTRIP TIMES TO: $hostname" -foregroundColor green
|
[int]$latency = GetPingLatency($hostname)
|
||||||
[int]$count = 1
|
if ($latency -eq 1500) {
|
||||||
do {
|
Write-Host "⚠️ Host '$hostname' is offline"
|
||||||
[float]$latency = GetPingLatency $hostname
|
} else {
|
||||||
Write-Host "#$count " -noNewline
|
Write-Host "✅ $hostname is up with $($latency)ms ping latency."
|
||||||
WriteChartLine $latency 1000.0 "$($latency)ms"
|
}
|
||||||
Start-Sleep -Milliseconds $timeInterval
|
|
||||||
$count++
|
|
||||||
} while($true)
|
|
||||||
exit 0 # success
|
exit 0 # success
|
||||||
} catch {
|
} catch {
|
||||||
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
Loading…
Reference in New Issue
Block a user