<# .SYNOPSIS This script helps you monitor packet loss on your network by continuously pinging a specified target (website or IP address) and providing real-time updates on successful responses and timeouts. .PARAMETER target Specifies the target website or IP address to ping. Defaults to "vc.voipoperator.de" if not specified. .PARAMETER count Specifies the number of times to ping the target. Defaults to 900 if not specified. .PARAMETER OnFailure (scriptblock, optional): A script block to execute when a ping attempt fails (i.e., when a timeout occurs). This script block will be executed in the context of the script. If not provided, no action will be taken on failure. .PARAMETER LogFile Specifies the log file path. Example: -LogFile "C:\logs\packetloss.txt" .INPUTS None .OUTPUTS Just output on screen, unless redirected to a file .NOTES Version: 1.1 Author: Heko Büroservice GmbH & Co. KG. Creation Date: 2025-03-10 .EXAMPLE .\packetloss_tester.ps1 -target "example.com" -count 1000 -OnFailure $failureAction -LogFile "C:\logs\packetloss.txt" #> param ( [string]$target = "vc.voipoperator.de", [int]$count = 900, [scriptblock]$OnFailure, [string]$LogFile # Optionaler Parameter für Logging ) $successCount = 0 $timeoutCount = 0 $processed = 0 $allTimeouts = @() $start = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Function Write-Log { param ( [string]$message ) if ($LogFile) { # In Log-Datei schreiben Add-Content -Path $LogFile -Value $message } # In Konsole ausgeben Write-Host $message } Function Invoke-Intro { Write-Log " _ _ _ ______ _ _ _ " Write-Log "| | | | | | | ___ (_) (_) (_) " Write-Log "| |_| | ___| | _____ | |_/ /_ _ _ __ ___ ___ ___ _ ____ ___ ___ ___ " Write-Log "| _ |/ _ \ |/ / _ \ | ___ \ | | | '__/ _ \/ __|/ _ \ '__\ \ / / |/ __/ _ \" Write-Log "| | | | __/ < (_) | | |_/ / |_| | | | (_) \__ \ __/ | \ V /| | (_| __/" Write-Log "\_| |_/\___|_|\_\___/ \____/ \__,_|_| \___/|___/\___|_| \_/ |_|\___\___|" Write-Log "" Write-Log "Packetloss Tester" Write-Log "by Max Krieger" Write-Log "heko-bs.de" Write-Log "" } Write-Log "$timestamp - Pinging $target ..." try { trap [System.Management.Automation.PipelineStoppedException] { Write-Log "`nSkript wurde abgebrochen. Zusammenfassung wird nicht ausgeführt." exit } for ($processed = 0; $processed -lt $count; $processed++) { $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $pingResult = Test-Connection -ComputerName $target -Count 1 -Quiet if ($pingResult) { $successCount++ $pingReply = Test-Connection -ComputerName $target -Count 1 if ($pingReply -ne $null) { Write-Log "[$($processed+1)/$count] $timestamp - Reply from $($pingReply.Address): bytes=$($pingReply.BufferSize) time=$($pingReply.ResponseTime)ms TTL=$($pingReply.TimeToLive)" } } else { $timeoutCount++ Write-Log "[$($processed+1)/$count] $timestamp - Request timed out" $allTimeouts += "[$($processed+1)/$count] $timestamp" if ($OnFailure) { Invoke-Command -ScriptBlock $OnFailure } } Start-Sleep -Seconds 1 # Pause für 1 Sekunde } } catch { Write-Log "`nEin Fehler ist aufgetreten: $_" } finally { if ($? -eq $false) { Write-Log "`nSkript wurde abgebrochen oder ein Fehler ist aufgetreten. Keine Zusammenfassung." exit } $packetLoss = [Math]::Round(($timeoutCount / $processed) * 100, 2) $totalTimeouts = $timeoutCount Invoke-Intro Write-Log "===== Zusammenfassung =====" Write-Log "From: $start || To: $timestamp" Write-Log "Total Packets sent: $processed" Write-Log "Packet loss: $packetLoss%" Write-Log "Total timeouts: $totalTimeouts" Write-Log "== Alle Timeouts: ==" foreach ($timeout in $allTimeouts) { Write-Log $timeout } Write-Log "===================" # Warte auf Tastendruck, nur wenn keine Datei geschrieben wird if (-not $LogFile) { Write-Host "Beliebige Taste zum Beenden drücken..." $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') } }