From 305077d5cd71c5c5f2611e3cdca4ae9daf5a76a9 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Tue, 1 Oct 2024 22:38:40 +0200 Subject: [PATCH] Improved ping-host.ps1 and write-headline.ps1 --- scripts/ping-host.ps1 | 20 ++++++++++++-------- scripts/write-headline.ps1 | 11 +++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/scripts/ping-host.ps1 b/scripts/ping-host.ps1 index 85c6f353..9ea51f79 100755 --- a/scripts/ping-host.ps1 +++ b/scripts/ping-host.ps1 @@ -1,6 +1,6 @@ <# .SYNOPSIS - Ping a host continuously + Ping a host .DESCRIPTION This PowerShell script pings the given host continously and shows the roundtrip times in a horizontal chart. .PARAMETER hostname @@ -9,11 +9,12 @@ Specifies the time interval in milliseconds to repeat the ping (1000 by default) .EXAMPLE PS> ./ping-host.ps1 - - Ping Roundtrip Times to Host: windows.com - ██████████████ 136ms - ████████████████ 154ms - █████████████████████████ 234ms + ----------------------------------------- + Ping Roundtrip Times to windows.com + ----------------------------------------- + #1 ██████████████ 136ms + #2 ████████████████ 154ms + #3 █████████████████████████ 234ms ... .LINK https://github.com/fleschutz/PowerShell @@ -36,7 +37,7 @@ function GetPingLatency([string]$hostname) { } function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text) - $num = ($value * 110.0) / $maxValue + $num = ($value * 108.0) / $maxValue while ($num -ge 1.0) { Write-Host -noNewLine "█" $num -= 1.0 @@ -60,11 +61,14 @@ function WriteChartLine { param([float]$value, [float]$maxValue, [string]$text) } try { - Write-Host "`nPing Roundtrip Times to Host: $($hostname)" -foregroundColor green + & "$PSScriptRoot/write-headline.ps1" "Ping Roundtrip Times to $($hostname)" + [int]$count = 1 do { [float]$latency = GetPingLatency $hostname + Write-Host "#$count " -noNewline WriteChartLine $latency 1000.0 "$($latency)ms" Start-Sleep -Milliseconds $timeInterval + $count++ } while($true) exit 0 # success } catch { diff --git a/scripts/write-headline.ps1 b/scripts/write-headline.ps1 index 5d660374..be1361a9 100755 --- a/scripts/write-headline.ps1 +++ b/scripts/write-headline.ps1 @@ -7,9 +7,9 @@ Specifies the text to write .EXAMPLE PS> ./write-headline.ps1 "Hello World" - - * Hello World * - --------------- + ----------------- + Hello World + ----------------- .LINK https://github.com/fleschutz/PowerShell .NOTES @@ -21,11 +21,10 @@ param([string]$text = "") try { if ($text -eq "") { $text = Read-Host "Enter the text to write" } - Write-Host "`n* $text *" -foregroundColor green [int]$len = $text.Length - [string]$line = "----" + [string]$line = "------" for ([int]$i = 0; $i -lt $len; $i++) { $line += "-" } - Write-Host "$line" -foregroundColor green + Write-Host "`n$line`n $text`n$line" -foregroundColor green exit 0 # success } catch { "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"