PowerShell function – check failed ping status (Request timed out)

There was often situation where I had to ping some server for specific time. This is why I created Get-Ping function where you can specify for how many minutes server will be pinged. At the end you will have full results in pop-up window.

In this function I just used simple ping command where I’ve searched lines with status names:

$ping = ping $Server -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"} 

One of function parameters is $Minutes. It will be used to specify end date for loop. In this example I used Do/Until:

$DateEnd = (Get-Date).AddMinutes($Minutes) 

Do
{
    $ping = ping $Server -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"} 
}
Until ((Get-Date) -ge $DateEnd )

Usage:

Get-Ping -Server DC01
Get-Ping -Server DC01 -Minutes 10

Output:

Get-Ping
Get-Ping

Final script:


Function Get-Ping {
    [CmdletBinding()]
        
    param
    (
        [Parameter(Position=0, Mandatory=$true, HelpMessage="Server name", ValueFromPipeline = $true)] 
        $Server,
 
        [Parameter(Position=1, Mandatory=$false, HelpMessage="For how many minutes", ValueFromPipeline = $true)] [Int]
        $Minutes = "1"
    ) 
 
        Write-Host "`nChecking $Server" -ForegroundColor Green

        $DateEnd = (Get-Date).AddMinutes($Minutes) 
        $ErrorActionPreference = 'Stop'
        $PingArray = @()
        $StartTime = Get-Date
 
        Write-Host "Start time: "$StartTime
   
        Do
        {
            Try
            {
                $ping = ping $Server -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"} 
                If (!$ping) {Throw "Failed to connect"}
            }
            Catch
            {
                Write-Host $_.Exception.Message -ForegroundColor Yellow
                Break
            }
 
            If($ping -match "Timed out")
            { 
                $date = ((Get-Date -Format F) + ' ' + [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1'))
                $status = $ping | Where-Object {$_ -match "Request timed out"} 
 
                Write-Host "$Server -"  " $status" : "$date "
             
                # Create a custom object 
                $Object = New-Object PSCustomObject
                $Object | Add-Member -MemberType NoteProperty -Name "Server" -Value $Server
                $Object | Add-Member -MemberType NoteProperty -Name "Tiemout date" -Value $date
                $Object | Add-Member -MemberType NoteProperty -Name "Status" -Value $status
 
                # Add custom object to our array
                $PingArray += $Object
 
            }
            ElseIf($ping -match "Destination host unreachable")
            {
                $date = ((Get-Date -Format F) + ' ' + [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])\w+\s*', '$1'))
                $status = $ping | Where-Object {$_ -match "Destination host unreachable"} 
             
                Write-Host "$Server -"  " $status" : "$date "
            }
        }
        Until ((Get-Date) -ge $DateEnd )
     
        $EndTime = Get-Date
        Write-Host "End time: "$EndTime
 
        If($PingArray)
        {
            $PingArray | Out-GridView -Title "Ping timeouts report"
        }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.