Ubidibity

ChatGPT-MultiPingLogger

Aug 26th, 2025 (edited)
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.61 KB | Software | 0 0
  1. # ChatGPT Multi target ping with log rotation to help isolate results during reported issues
  2. # second revision added a second failure only file, and console output during execution
  3. # Configurable variables
  4. $Targets     = @("192.168.1.1","192.168.1.10","8.8.8.8")   # Routers, switches, VM, etc
  5. $Frequency   = 5      # seconds between samples of the SAME host
  6. $Persistent  = $true  # $true = run until stopped, $false = stop after $Duration
  7. $Duration    = 600    # seconds total runtime (ignored if $Persistent = $true)
  8.  
  9. # Function: get current hourly log file name
  10. function Get-HourlyLogFile {
  11.     $timestamp = Get-Date -Format "yyyyMMddHH"
  12.     $logfile   = ".\pinglog_$timestamp.csv"
  13.     if (-not (Test-Path $logfile)) {
  14.         "Timestamp,Target,Status,Latency_ms" | Out-File -FilePath $logfile -Encoding utf8
  15.     }
  16.     return $logfile
  17. }
  18.  
  19. # Function: get current daily packet loss log file name
  20. function Get-DailyLossLog {
  21.     $timestamp = Get-Date -Format "yyyyMMdd"
  22.     $lossfile  = ".\packetloss_$timestamp.csv"
  23.     if (-not (Test-Path $lossfile)) {
  24.         "Timestamp,Target" | Out-File -FilePath $lossfile -Encoding utf8
  25.     }
  26.     return $lossfile
  27. }
  28.  
  29. # Function: run one ping attempt
  30. function Test-Target ($target) {
  31.     $result = Test-Connection -ComputerName $target -Count 1 -ErrorAction SilentlyContinue
  32.     $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  33.     if ($null -ne $result) {
  34.         $latency = "{0:N2}" -f $result.ResponseTime
  35.         $data = [PSCustomObject]@{
  36.             Timestamp   = $timestamp
  37.             Target      = $target
  38.             Status      = "Success"
  39.             Latency_ms  = $latency
  40.         }
  41.     }
  42.     else {
  43.         $data = [PSCustomObject]@{
  44.             Timestamp   = $timestamp
  45.             Target      = $target
  46.             Status      = "Timeout"
  47.             Latency_ms  = ""
  48.         }
  49.         # Also log timeout to packet loss log
  50.         $losslog = Get-DailyLossLog
  51.         "$timestamp,$target" | Out-File -FilePath $losslog -Append -Encoding utf8
  52.     }
  53.     # Console output
  54.     Write-Host ("{0} | {1,-15} | {2,-7} | {3}" -f $data.Timestamp,$data.Target,$data.Status,$data.Latency_ms)
  55.     return $data
  56. }
  57.  
  58. # Main loop
  59. $start = Get-Date
  60. $index = 0
  61.  
  62. while ($true) {
  63.     $target = $Targets[$index % $Targets.Count]
  64.     $data   = Test-Target $target
  65.     $log    = Get-HourlyLogFile
  66.     $data | Export-Csv -Path $log -Append -NoTypeInformation
  67.     Start-Sleep -Seconds $Frequency
  68.  
  69.     if (-not $Persistent) {
  70.         $elapsed = (New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
  71.         if ($elapsed -ge $Duration) { break }
  72.     }
  73.     $index++
  74. }
  75.  
Advertisement