Just wrote a small PowerShell function that can output ping results to a file including timeouts and unreachable information. This is not something the native Test-NetConnection cmdlet can do (prove me wrong?) unfortunately.
Use the function free of charge.
The function will run forever. There are 3 options to set:
- Destination = IP or Hostname (mandatory).
- OutputFolder = Sets the output folder, for the CSV file (optional).
- OutputToScreen = Output the result to the screen if possible, true/false (optional).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
Function Invoke-Ping { [CmdletBinding()] Param ( #Validate hostname. [Parameter(Mandatory=$true, ValueFromPipeline = $true)] [ValidatePattern('^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$')] $Destination, [Parameter(Mandatory=$false, ValueFromPipeline = $true)] $OutputFolder, [Parameter(Mandatory=$false, ValueFromPipeline = $true)] [boolean]$OutputToScreen ) #Counter. [uint64]$Counter = 0; #Object array. $Pings = @(); #Create endless loop. While($true) { Try { #Ping the target server, filter on output. $Ping = ping $Destination -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"}; #Check after output in the ping reponse. If(!($Ping)) { #If the ping output doesn't match. Throw "Failed to connect."; } } Catch { #Output exception. Write-Host ($_.Exception.Message) -ForegroundColor Yellow; #Break. Break; } #Add to acounter. $Counter++; #Get source IP-address. $Source = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]; #Get date and time. $Date = Get-Date; #Create a new object. $PingObject = New-Object -TypeName PSObject; #Get the reponse time. If(!($Time = ((($Ping -split "time=")[1]) -split " ")[0])) { #If the time isn't set. $Time = 0 } #Add value to the object. Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Id" -Value ($Counter); Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "DateTime" -Value ($Date.ToString("o")); Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Source" -Value ($Source); Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Destination" -Value ($Destination); #If the ICMP package times out. If($Ping -match "Timed out") { Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Status" -Value "Timeout"; Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Time" -Value ("N/A"); } #If the destination is unreachable. Elseif($Ping -match "Destination net unreachable") { Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Status" -Value "Unreachable"; Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Time" -Value ("N/A"); } #Else if the reply is OK. Else { Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Status" -Value "Acknowledgement"; Add-Member -InputObject $PingObject -Membertype NoteProperty -Name "Time" -Value ($Time); } #Add the object to the array. $Pings += $PingObject; #Append result to file. If($OutputFolder -ne $null) { #Generate output file. $OutputFile = ($OutputFolder + "\" + $Destination + "_" + $Date.Day + "-" + $Date.Month + "-" + $Date.Year + ".csv"); #Append to CSV file. $PingObject | Export-Csv -Path ($OutputFile) -Encoding UTF8 -Delimiter ";" -NoTypeInformation -Force -Append; } #Output to screen if the option is set. If($OutputToScreen -eq $true) { #Output to screen. $PingObject | Format-Table; } #Start delay. Start-Sleep -Seconds 1; } } Invoke-Ping -Destination <target IP> -OutputFolder "C:\Users\<username>\Desktop" -OutputToScreen $false |