Ping.ps1

<#PSScriptInfo
 
.VERSION 1.1.0
 
.GUID 81a98bc4-d077-4dd7-ac9c-4a19a8c3246d
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
ping
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
 Fast alternate ping.exe
  
#>
 
[CmdletBinding(DefaultParameterSetName='Limited')]
param(
        [Parameter(Mandatory=$true,Position=0)][alias('IPAddress')]$HostAddress,
        [Parameter(ParameterSetName='Limited')][ValidateRange(1,1099511627776)][alias('n')][Int64]$Count = 5,
        [Parameter(ParameterSetName='Unlimited')][alias('t')][switch]$infinity,
        [ValidateRange(1,10000)][alias('w','TimeOut')][int]$Delay = 255,
        [ValidateRange(1,65500)][alias('l','Size','Length')][int]$BufferSize = 16,
        [ValidateRange(1,255)][alias('i','TTL')][int]$TimeToLive = 255,
        [ValidateRange(0,10000)][alias('Throttle','ms')][int]$ThrottleMilliseconds = 255,
        [switch][alias('DateTime')]$Timestamp,
        [switch][alias('df','f')]$DontFragment,
        [switch]$Fast,
        [switch]$Graphic,
        [ValidateRange(1,100)][int]$WindowSize = 100,
        [switch][alias('a')]$ResolveDNS
    )
    
    [Hashtable[]]$select = @(
        ,@{Name = 'Number'; Expression = {$i}}
        ,@{Name = 'TTL'; Expression = {$_.Options.ttl}}
        ,@{Name = 'Status'; Expression = {$_.Status}}
        ,@{Name = 'DontFragment'; Expression = {$_.Options.DontFragment}}
        ,@{Name = 'Address'; Expression = {$_.Address}}
        ,@{Name = 'RoundtripTime'; Expression = {$_.RoundtripTime}}
    )
    if ($infinity) {$Count = 1tb}
    if ($Fast) {$ThrottleMilliseconds = 0}
    if ($Timestamp) {$select += @{Name = 'Timestamp'; Expression = {Get-Date}}}
    if ($ResolveDNS) {$select += @{Name = 'HostnamePTR'; Expression = {[System.Net.Dns]::GetHostEntry($_.Address).Hostname}}}

    
    if (!($HostAddress -as [System.Net.IPAddress])){
        $HostIP = [System.Net.Dns]::GetHostEntry($HostAddress).AddressList.IPAddressToString | Get-Random
        if(!$HostIP){break}
    } else {
        $HostIP = [System.Net.IPAddress]$HostAddress
    }
    
    [byte[]]$Buffer = [byte[]](1)*$BufferSize
    $GraphicStat = @()
    for($i = 1; $i -le $Count; $i++) {
        $Ping = New-Object -TypeName System.Net.NetworkInformation.Ping
        $PingOptions = New-Object -TypeName System.Net.NetworkInformation.PingOptions -ArgumentList @($TimeToLive,$DontFragment)
        $Ping.Send($HostIP, $Delay, $Buffer, $PingOptions) | Select-Object -Property $select | % {
            if (!$Graphic) {
                $_.psobject.Typenames.Insert(0,'NetWork.Ping')
                $_
            } else {
                $GraphicStat += $_
                Write-Host -Object @{'Success'='!';'TimedOut'='.'}[$_.Status.ToString()] -NoNewline -ForegroundColor Yellow
                if ($_.Number % $WindowSize -eq 0) {
                    $GraphicLine = $GraphicStat | Group-Object -Property Status -AsHashTable -AsString
                    Write-Host -Object $(' : '+([string]$GraphicLine['Success'].Count).PadLeft(3,' ')+' : '+(Get-Date).ToString('HH:mm:ss')) -NoNewline -ForegroundColor Yellow
                    Write-Host -Object ''
                    $GraphicStat = @()
                }
            }
            if ($ThrottleMilliseconds) {Start-Sleep -Milliseconds $ThrottleMilliseconds}
        }
    }