Get-PingMonitor.ps1

something exciting

Some information about the exciting thing

Table of contents generated with markdown-toc


Script

[CmdletBinding()]
Param (
    [int32]$Count = 5,
    [Parameter(ValueFromPipeline = $true)]
    [String[]]$ComputerName,
    [string]$LogPath = "c:\pinglog\pinglog.csv"
)

$Ping = @()
#Test if path exists, if not, create it
If (-not (Test-Path (Split-Path $LogPath) -PathType Container)) {
    Write-Verbose "Folder doesn't exist $(Split-Path $LogPath), creating..."
    New-Item (Split-Path $LogPath) -ItemType Directory | Out-Null
}

#Test if log file exists, if not seed it with a header row
If (-not (Test-Path $LogPath)) {
    Write-Verbose "Log file doesn't exist: $($LogPath), creating..."
    Add-Content -Value '"TimeStamp","Source","Destination","IPV4Address","Status","ResponseTime"' -Path $LogPath
}

#Log collection loop
Write-Verbose "Beginning Ping monitoring of $ComputerName for $Count tries:"
While ($Count -gt 0) {
    $Ping = Get-WmiObject Win32_PingStatus -Filter "Address = '$ComputerName'" | Select-Object @{Label = "TimeStamp"; Expression = { Get-Date } }, @{Label = "Source"; Expression = { $_.__Server } }, @{Label = "Destination"; Expression = { $_.Address } }, IPv4Address, @{Label = "Status"; Expression = { If ($_.StatusCode -ne 0) { "Failed" } Else { "" } } }, ResponseTime
    $Result = $Ping | Select-Object TimeStamp, Source, Destination, IPv4Address, Status, ResponseTime | ConvertTo-Csv -NoTypeInformation
    $Result[1] | Add-Content -Path $LogPath
    Write-verbose ($Ping | Select-Object TimeStamp, Source, Destination, IPv4Address, Status, ResponseTime | Format-Table -AutoSize | Out-String)
    $Count --
    Start-Sleep -Seconds 1
}

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top