This script takes a target IP address or hostname and pings it, providing the source IP address the stack uses to ping that host. If the DNS server returns multiple records, it will ping them all. Note that this script uses the [System.Net.Dns]::GetHostAddresses function, this returns an array of IP addresses. If you want access to the complete DNS record objects, it is recommended that you use the Resolve-DnsName cmdlet. An commented example is provided below. This script outputs 2 objects, the ping results and the ping statistics.
In addition, this script may be used as an example of how to use System.NET to enhance your PowerShell scripts.
Note that the below code snippet sends a ping, and queries the IP stack for the source address.
function SendPing($Destination)
{
$TempResult = CreateResultObject;
$TempResult.RemoteIPAddress = $Destination
####################################
#Send the ping
try
{
$Result = $IcmpRequest.send($Destination)
####################################
#The amount of time to wait between pings
Start-Sleep -Milliseconds $TimeBetweenPings
}
catch [System.Management.Automation.MethodException]
{
#If result is null record a general failure, this is handled below
}
####################################
#If a result was observed, record it
if($Result)
{
$TempResult.Size = $Result.Buffer.Length
$TempResult.TTL = $Result.Options.TTL
$TempResult.Status = $Result.Status
if($Result.Status -eq "Success")
{
$TempResult.RTT = $Result.RoundtripTime
if($Result.RoundtripTime -eq 0)
{
$TempResult.RTT = "<1"
}
}
if($Result.Status -ne "Success")
{
$TempResult.RTT = $Null
$TempResult.Size = $Null
}
if($Result.Address -ne $Null)
{
$TempResult.RemoteIPAddress = $Result.Address.ToString()
}
}
####################################
#If a result was not observed, record it
if(!$Result)
{
$TempResult.Status = "GeneralFailure"
$TempResult.RTT = $Null
$TempResult.Size = $Null
$TempResult.TTL = $Null
}
################################################
#Query the IP stack to calculate the source address of the ping.
if($TempResult.Status -ne "GeneralFailure")
{
try
{
$UdpClient = new-object System.Net.Sockets.UdpClient($Destination, 6000)
$LocalAddress = $UdpClient.Client.LocalEndPoint
if($UdpClient.Client.LocalEndPoint.AddressFamily -eq "InterNetwork")
{
$LocalAddress = $LocalAddress.ToString()
$TempAddress = $LocalAddress.Split(":")
$TempResult.LocalIPAddress = [string]$TempAddress[0]
}
if($UdpClient.Client.LocalEndPoint.AddressFamily -eq "InterNetworkV6")
{
$LocalAddress = $LocalAddress.ToString()
$TempAddress = $LocalAddress.Split("]")
$LocalAddress = [string]$TempAddress[0]
$TempAddress = $LocalAddress.Split("[")
$TempResult.LocalIPAddress = [string]$TempAddress[1]
}
}
catch
{
$LocalAddress = ""
}
finally
{
if($UdpClient)
{
$UdpClient.Close()
}
}
}
Return $TempResult
}
function SendPing($Destination) { $TempResult = CreateResultObject; $TempResult.RemoteIPAddress = $Destination #################################### #Send the ping try { $Result = $IcmpRequest.send($Destination) #################################### #The amount of time to wait between pings Start-Sleep -Milliseconds $TimeBetweenPings } catch [System.Management.Automation.MethodException] { #If result is null record a general failure, this is handled below } #################################### #If a result was observed, record it if($Result) { $TempResult.Size = $Result.Buffer.Length $TempResult.TTL = $Result.Options.TTL $TempResult.Status = $Result.Status if($Result.Status -eq "Success") { $TempResult.RTT = $Result.RoundtripTime if($Result.RoundtripTime -eq 0) { $TempResult.RTT = "<1" } } if($Result.Status -ne "Success") { $TempResult.RTT = $Null $TempResult.Size = $Null } if($Result.Address -ne $Null) { $TempResult.RemoteIPAddress = $Result.Address.ToString() } } #################################### #If a result was not observed, record it if(!$Result) { $TempResult.Status = "GeneralFailure" $TempResult.RTT = $Null $TempResult.Size = $Null $TempResult.TTL = $Null } ################################################ #Query the IP stack to calculate the source address of the ping. if($TempResult.Status -ne "GeneralFailure") { try { $UdpClient = new-object System.Net.Sockets.UdpClient($Destination, 6000) $LocalAddress = $UdpClient.Client.LocalEndPoint if($UdpClient.Client.LocalEndPoint.AddressFamily -eq "InterNetwork") { $LocalAddress = $LocalAddress.ToString() $TempAddress = $LocalAddress.Split(":") $TempResult.LocalIPAddress = [string]$TempAddress[0] } if($UdpClient.Client.LocalEndPoint.AddressFamily -eq "InterNetworkV6") { $LocalAddress = $LocalAddress.ToString() $TempAddress = $LocalAddress.Split("]") $LocalAddress = [string]$TempAddress[0] $TempAddress = $LocalAddress.Split("[") $TempResult.LocalIPAddress = [string]$TempAddress[1] } } catch { $LocalAddress = "" } finally { if($UdpClient) { $UdpClient.Close() } } } Return $TempResult }