Last active
April 2, 2018 17:56
-
-
Save jimmitt/fc2cae83d3b4ff783e69c9751eba459f to your computer and use it in GitHub Desktop.
Pings an address and saves the results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Created By James Trimble, 29 March 2018 - http://www.jamestrimble.info | |
#Based on a script by Martin9700 posted to a forum on 17 May 2013, https://community.spiceworks.com/topic/337701-ping-via-powershell-log-results-with-timestamp | |
Param ( | |
[int32]$count = 5, | |
[Parameter(ValueFromPipeline=$true)] | |
[String[]]$destination = "8.8.8.8", | |
[Int]$threshold = 50, | |
[string]$resultsFile = ".\pingResults.csv", | |
[string]$dataFile = ".\pingData.csv" | |
) | |
$Ping = @() | |
#Test if path exists, if not, create it | |
If (-not (Test-Path (Split-Path $dataFile) -PathType Container)) | |
{ Write-Verbose "Folder doesn't exist $(Split-Path $dataFile), creating..." | |
New-Item (Split-Path $dataFile) -ItemType Directory | Out-Null | |
} | |
#Test if data file exists, if not seed it with a header row | |
If (-not (Test-Path $dataFile)) | |
{ Write-Verbose "Log file doesn't exist: $($dataFile), creating..." | |
Add-Content -Value '"TimeStamp","Source","Destination","IPV4Address","Status","ResponseTime","AboveThreshold"' -Path $dataFile | |
} | |
#Test if results file exists, if not seed it with a header row | |
If (-not (Test-Path $resultsFile)) | |
{ Write-Verbose "Log file doesn't exist: $($resultsFile), creating..." | |
Add-Content -Value '"StartTime","StopTime","IPV4Address","DroppedPackets","AboveThreshold"' -Path $resultsFile | |
} | |
#Log collection loop | |
Write-Verbose "Beginning Ping monitoring of $destination for $count tries:" | |
$droppedPackets = 0; | |
$aboveThreshold = 0; | |
$startTime = ""; | |
$stopTime = ""; | |
While ($count -gt 0) { | |
$pingResult = Get-WmiObject Win32_PingStatus -Filter "Address = '$destination'" | Select @{Label="TimeStamp";Expression={Get-Date}},@{Label="Source";Expression={ $_.__Server }},@{Label="Destination";Expression={ $_.Address }},IPv4Address,@{Label="Status";Expression={ If ($_.StatusCode -ne 0) {"Failed"} Else {"Passed"}}},ResponseTime,@{Label="AboveThreshold";Expression={ If ($_.ResponseTime -gt $threshold) {"Yes"} Else {""}}} | |
if ($startTime -eq "") { | |
$startTime = $pingResult.TimeStamp.ToString("s"); | |
} | |
$stopTime = $pingResult.TimeStamp.ToString("s"); | |
if ($pingResult.AboveThreshold -eq "Yes") { | |
$aboveThreshold++; | |
} elseif ($pingResult.Status -eq "Failed") { | |
$aboveThreshold++; | |
$droppedPackets ++; | |
} | |
if ($pingResult.AboveThreshold -eq "Yes" -or $pingResult.Status -eq "Failed") { | |
#Write-verbose ($pingResult | Select TimeStamp,Source,Destination,IPv4Address,Status,ResponseTime,AboveThreshold | Format-Table -AutoSize | Out-String) | |
$dataResult = $pingResult | Select TimeStamp,Source,Destination,IPv4Address,Status,ResponseTime,AboveThreshold | ConvertTo-Csv -NoTypeInformation | |
$dataResult[1] | Add-Content -Path $dataFile | |
} | |
if ($pingResult.Status -eq "Failed") { | |
"Request timed out." | Out-String | |
} else { | |
$aboveMsg = ""; | |
if ($pingResult.AboveThreshold -eq "Yes") { | |
$aboveMsg = " <<< Above Threshold"; | |
} | |
$pingResult.TimeStamp.ToString()+": Reply from "+$destination+": time="+$pingResult.ResponseTime.ToString()+"ms"+$aboveMsg | Out-String | |
} | |
$count -- | |
Start-Sleep -Seconds 1 | |
} | |
$resultsResult = $startTime+","+$stopTime+","+$destination+","+$droppedPackets.ToString()+","+$aboveThreshold.ToString(); | |
$resultsResult | Add-Content -Path $resultsFile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment