Functions/Networking/Test-Ping.ps1

Function Test-Ping
    {
    [CmdletBinding()]
    Param
        (
        # Name of Computer to Ping
        [Parameter(Mandatory=$true)]
        [String]
        $ComputerName,
        
        # Timeout to use in ping attempt
        [Parameter(Mandatory=$false)]
        [Int]
        $Timeout = 20,
        
        # TTL to use in ping attempt
        [Parameter(Mandatory=$false)]
        [Int]
        $TTL = 128,

        # BufferSize to use in ping attempt
        [Parameter(Mandatory=$false)]
        [Int]
        $BufferSize = 32,
        
        # Resolve Address?
        [Parameter(Mandatory=$false)]
        [switch]
        $resolve = $true,

        # Don't Fragment Packet?
        [Parameter(Mandatory=$false)]
        [Switch]
        $DontFragment = $false
        )
    Begin
        {
        $options = new-object system.net.networkinformation.pingoptions
        $options.TTL = $TTL
        $options.DontFragment = $DontFragment
        $buffer=([system.text.encoding]::ASCII).getbytes("a"*$buffersize)
        }
    Process
        {
        # Construct Ping Object and execute send method
        $ping = new-object system.net.networkinformation.ping
        try{$reply = $ping.Send($ComputerName,$timeout,$buffer,$options)}
        catch{$ErrorMessage = $_.Exception.Message}

        # Create Result Object
        $PingSucceeded = if($reply.status -eq "Success"){$True}else{$false}
        [pscustomobject](@{
            ComputerName = $ComputerName
            PingSucceeded = $PingSucceeded
            })
        }
    }