KOD İÇERİĞİ : ( ping_v2_1.ps1)
Kod: Tümünü seç
#!/usr/bin/env pwsh
# ============================================================
# Ping Connection Quality Tool v2.1
# Compatible with Windows 10/11, Windows PowerShell 5.1 and PowerShell 7+
# Supports both interactive use and scripted/parameterized invocation.
# ============================================================
param(
[string]$InputHost,
[int]$Count = 6,
[int]$RttRef = 200,
[int]$TimeoutMs = 4000,
[int]$DelayMs = 250
)
# Track whether the host was supplied on the command line (scripted use) or
# had to be prompted for interactively - this determines whether the script
# pauses for a keypress at the end. Pausing during scripted/automated
# invocation would make the calling script hang, so the pause only happens
# when a human was actually sitting at this console.
$isInteractive = -not $PSBoundParameters.ContainsKey('InputHost')
if (-not $InputHost) {
$InputHost = Read-Host "Please enter an IP address, host name, or URL"
}
if (-not $InputHost) {
Write-Host "No IP/host was entered." -ForegroundColor Red
exit 1
}
# =====================================================
# VALIDATE NUMERIC PARAMETERS
# =====================================================
if ($Count -le 0) {
Write-Host "[WARNING] Count must be greater than 0. Using the default (6)." -ForegroundColor Yellow
$Count = 6
}
if ($RttRef -le 0) {
Write-Host "[WARNING] RttRef must be greater than 0. Using the default (200)." -ForegroundColor Yellow
$RttRef = 200
}
if ($TimeoutMs -le 0) { $TimeoutMs = 4000 }
# =====================================================
# EXTRACT THE HOST FROM A URL/HOST:PORT INPUT
# =====================================================
$targetHost = $InputHost
try {
$u = [uri]::new($InputHost)
if ($u.Host) { $targetHost = $u.Host }
}
catch {
$targetHost = $InputHost
}
# Strip a trailing ":port" - but only for plain host:port, not raw IPv6
# addresses (which contain multiple colons and are not currently handled
# with bracket notation here).
if (($targetHost -notmatch ':.*:') -and ($targetHost -match "^(.*?):\d+$")) {
$targetHost = $Matches[1]
}
# =====================================================
# RESOLVE THE HOST FIRST (fast, clear failure for bad names)
# =====================================================
try {
[System.Net.Dns]::GetHostAddresses($targetHost) | Out-Null
}
catch {
Write-Host "Host could not be resolved: $targetHost" -ForegroundColor Red
if ($isInteractive) { Read-Host "`nPress ENTER to exit." }
exit 1
}
# =====================================================
# PING LOOP (uses System.Net.NetworkInformation.Ping directly instead of
# Test-Connection, since Test-Connection's returned object type and property
# names differ between Windows PowerShell 5.1 (Win32_PingStatus.ResponseTime)
# and PowerShell 7+ (PingStatus.Latency) - relying on either property name
# silently breaks on the other PowerShell version. Ping.Send() has an
# identical API on both. This also allows genuine per-packet partial-loss
# measurement, which -ErrorAction Stop on Test-Connection does not: a single
# dropped packet there aborts the entire test instead of just counting as
# one lost packet.)
# =====================================================
$pingClient = [System.Net.NetworkInformation.Ping]::new()
$rtts = [System.Collections.Generic.List[double]]::new()
$received = 0
Write-Host "Pinging $targetHost ($Count packets)..." -ForegroundColor Cyan
for ($i = 1; $i -le $Count; $i++) {
try {
$reply = $pingClient.Send($targetHost, $TimeoutMs)
if ($reply.Status -eq [System.Net.NetworkInformation.IPStatus]::Success) {
$received++
$rtts.Add([double]$reply.RoundtripTime)
Write-Host (" [{0}/{1}] Reply from {2}: time={3} ms" -f $i, $Count, $reply.Address, $reply.RoundtripTime) -ForegroundColor Green
}
else {
Write-Host (" [{0}/{1}] {2}" -f $i, $Count, $reply.Status) -ForegroundColor Yellow
}
}
catch {
Write-Host (" [{0}/{1}] Request failed: {2}" -f $i, $Count, $_.Exception.Message) -ForegroundColor Yellow
}
if ($i -lt $Count) { Start-Sleep -Milliseconds $DelayMs }
}
# =====================================================
# CALCULATIONS
# =====================================================
$sent = $Count
$lossPct = [math]::Round((1 - ($received / $sent)) * 100, 4)
$successPct = [math]::Round(($received / $sent) * 100, 2)
if ($rtts.Count -gt 0) {
$avgRtt = [math]::Round((($rtts | Measure-Object -Average).Average), 3)
$minRtt = [math]::Round((($rtts | Measure-Object -Minimum).Minimum), 3)
$maxRtt = [math]::Round((($rtts | Measure-Object -Maximum).Maximum), 3)
$rttRatio = [math]::Min($avgRtt / $RttRef, 1)
$rttFactor = [math]::Round(1 - $rttRatio, 4)
}
else {
$avgRtt = $null
$minRtt = $null
$maxRtt = $null
$rttFactor = 0
}
$perf = [math]::Round(($successPct / 100) * $rttFactor * 100, 2)
# --- Connection quality assessment ---
if ($lossPct -ge 20) {
$qualityText = "CRITICAL"; $qualityColor = "Red"
}
elseif ($lossPct -ge 5) {
$qualityText = "PROBLEMATIC"; $qualityColor = "Yellow"
}
elseif ($null -ne $avgRtt -and $avgRtt -ge ($RttRef * 2)) {
$qualityText = "HIGH LATENCY"; $qualityColor = "Yellow"
}
elseif ($lossPct -gt 0 -or ($null -ne $avgRtt -and $avgRtt -ge $RttRef)) {
$qualityText = "GOOD"; $qualityColor = "Green"
}
elseif ($received -eq 0) {
$qualityText = "UNREACHABLE"; $qualityColor = "Red"
}
else {
$qualityText = "EXCELLENT"; $qualityColor = "Green"
}
# =====================================================
# OUTPUT
# =====================================================
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "PING RESULTS" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ("Host : {0}" -f $targetHost)
Write-Host ("Sent packets : {0}" -f $sent)
Write-Host ("Received packets : {0}" -f $received)
Write-Host ("Packet loss : {0}%" -f $lossPct)
Write-Host ""
if ($null -ne $avgRtt) {
Write-Host ("Average RTT : {0} ms (min {1} / max {2}, reference {3} ms)" -f $avgRtt, $minRtt, $maxRtt, $RttRef)
}
else {
Write-Host "RTT information : not available (no successful replies)" -ForegroundColor Yellow
}
Write-Host ("Raw success rate : {0}%" -f $successPct)
Write-Host ("RTT factor : {0}" -f $rttFactor)
Write-Host ("Connection score : {0}%" -f $perf)
Write-Host ""
Write-Host ("Connection Quality: {0}" -f $qualityText) -ForegroundColor $qualityColor
Write-Host "========================================" -ForegroundColor Cyan
if ($isInteractive) {
Write-Host ""
Read-Host "`nPress ENTER to exit."
}
Kod: Tümünü seç
param(
[string]$InputHost,
[int]$Count = 6,
[int]$RttRef = 200,
[int]$TimeoutMs = 4000,
[int]$DelayMs = 250
)EKRAN GÖRÜNTÜSÜ :



