Test-PingStatistic.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 7df74eb2-f963-4923-851c-b687e9194d80
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS Ping ICMP IP Statistic
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Ping Statistics with SoundPlay
 Unlimited loop by default
 For Very fast ping use parameter "Delay"
 
.LINK
 https://sawfriendship.wordpress.com/
 
.EXAMPLE
 Test-PingStatistic ya.ru | ft
 
.EXAMPLE
 Test-PingStatistic ya.ru -MuteSuccess -MuteError -Delay 50 -NoMeasureStat
 
.EXAMPLE
 Get-NetNeighbor 192.*.*.1 | Test-PingStatistic
 
 
#>
 

[CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][Alias("IP","Host")][string]$IPAddress,
        [ValidateRange(1,65500)][int]$BufferSize = 16,
        [ValidateRange(1,255)][int]$TimeToLive = 80,
        [ValidateRange(0,10000000000)][int]$MaxCount = 0,
        [ValidateRange(1,100000)][int]$Delay = 1000,
        [switch]$MuteSuccess,
        [switch]$MuteError,
        [switch]$NoDisplay,
        [switch]$NoWriteProgress,
        [switch]$NoMeasureStat,
        [ValidateRange(100,1000000)][int]$MeasureLast = 10000
    )
    
    [int]$i = 0
    [int]$SuccessCount = 0
        
    while ($True) { if($MaxCount -ne 0 -and $i -ge $MaxCount) {break} else {$i++}
    
        $Test = Test-Connection $IPAddress -Count 1 -BufferSize $BufferSize -TimeToLive $TimeToLive -ErrorAction SilentlyContinue
        
        if(!$NoMeasureStat){
            $TestStat = (@($TestStat) + $Test) | select -Last $MeasureLast
            $MeasureResponseTime = $TestStat | Measure-Object -Property ResponseTime -Average -Maximum -Minimum
        }
        
        
        if ($Test.StatusCode -eq [int]0) {
            $SuccessCount++
                if(!$NoDisplay){
                    [pscustomobject][ordered]@{
                        index = $i
                        ProtocolAddress = $Test.ProtocolAddress
                        StatusCode = $Test.StatusCode
                        ResponseTime = $Test.ResponseTime
                        ResponseTimeToLive = $Test.ResponseTimeToLive
                        ReplySize = $Test.ReplySize
                        SuccessPercent = [System.Math]::Round(($SuccessCount/$i*100),2)
                        DateTime = Get-Date
                    }
                }
                if(!$MuteSuccess -and $Delay -gt 400){
                    [System.Console]::Beep(440,500)
                }
                
            } else {
                if(!$NoDisplay){
                    [pscustomobject][ordered]@{
                        index = $i
                        ProtocolAddress = $Test.ProtocolAddress
                        StatusCode = 1
                        ResponseTime = $Test.ResponseTime
                        ResponseTimeToLive = $Test.ResponseTimeToLive
                        ReplySize = $Test.ReplySize
                        SuccessPercent = [System.Math]::Round(($SuccessCount/$i*100),2)
                        DateTime = Get-Date
                    }
                }
                if(!$MuteError -and $Delay -gt 400){
                    [System.Console]::Beep(1760,250)
                }
            }
        
        if(!$NoWriteProgress -and !$NoMeasureStat){Write-Progress -Activity "Ping sequence number: $i" -Status ("Average: $([System.Math]::Round(($MeasureResponseTime.Average),2) ) ; Maximum: $([System.Math]::Round(($MeasureResponseTime.Maximum),2) ) ; Minimum: $([System.Math]::Round(($MeasureResponseTime.Minimum),2) )") -CurrentOperation ("SuccessPercent: $([System.Math]::Round(($SuccessCount/$i*100),2))")}
        Start-Sleep -Milliseconds $Delay
        
    }