<pre class=“brush: powershell; gutter: true; first-line: 1; highlight: []; html-script: false”>
#region Function Test-Ping
Function Test-Ping {
<#
.SYNOPSIS
Pings IP or DNS Name
.DESCRIPTION
Sends ICMP echo request packets ("pings") to one or more computers using .Net's [system.net.networkinformation.ping]
Returns $true or $false
This function tries to mimic PS v4's Test-connection somewhat
FYI: PSADT limits itself to PS v2 and .net 2.0 for maximum compatibility
.PARAMETER ComputerName
CN,IPAddress,__SERVER,Server,Destination
Name or IP the target computer.
.PARAMETER Count
Number of echo ICMP requests sent. The default value is 4.
.PARAMETER BufferSize
Buffer used with this command. Default is 32 bytes.
.PARAMETER TimeToLive
Maximum number of times the ICMP echo message can be forwarded before reaching its destination.
If the packet does not reach its destination after being forwarded the specified number of times,
the packet is discarded and the ICMP echo request fails. When this happens, the Status is set to TtlExpired.
Range is 1-255. Default is 64
.PARAMETER TimeOut
Maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message from target.
.PARAMETER DontFragment
If true and the total packet size exceeds the maximum packet size that can be transmitted by one of the routing
nodes between the local and remote computers, the ICMP echo request fails.
When this happens, the Status is set to PacketTooBig.
.PARAMETER PassThru
Returns a PSObject.
Use the following to obtain list of properties that are available:
Test-Ping -Name $envComputerName -PassThru | select *
.PARAMETER ContinueOnError
Continue if an error is encountered. Default is: $true.
.EXAMPLE
Test-Ping -ComputerName 'MyServerName' -PassThru
.EXAMPLE
Test-Ping -ComputerName '10.10.0.31' -PassThru
.EXAMPLE
Test-Ping 'dontexixt'
(Returns $false)
.NOTES
Tested on Win7 english. Should work on XP and up.
Depends on Write-log and Write-FunctionHeaderOrFooter
CAVEAT: If given %ComputerName% as target, will use IP6 to ping
.LINK
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, HelpMessage="CN,IPAddress,__SERVER,Server,Destination")]
[Alias('CN','__SERVER','IPAddress','Server')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, HelpMessage="Number of echo ICMP requests sent. The default value is 4")]
[ValidateNotNullOrEmpty()]
[Int]$Count = 4,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[Int]$BufferSize = 32,
[Parameter(Mandatory=$false, HelpMessage="Number of hops before it gives up")]
[ValidateNotNullOrEmpty()]
[Alias('TTL')]
[Int]$TimeToLive = 64,
[Parameter(Mandatory=$false, HelpMessage="In milliseconds")]
[ValidateNotNullOrEmpty()]
[Int32]$timeout = 1000,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[Bool]$DontFragment = $false,
[Parameter(Mandatory=$false, HelpMessage="Returns a PSObject instead of [Bool]")]
[ValidateNotNullOrEmpty()]
[switch]$PassThru
)
Begin {
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -CmdletBoundParameters $PSBoundParameters -Header
}
Process {
Try {
[Bool]$resolve = $true
[system.net.NetworkInformation.PingOptions]$options = new-object system.net.networkinformation.pingoptions
$options.TTL = $TimeToLive
$options.DontFragment = $DontFragment
[array]$buffer = ([system.text.encoding]::ASCII).getbytes("a"*$buffersize)
Try {
#Must test $ComputerName to make sure it converts to a IP address or else .Ping will blow up
$IpAddress = [System.Net.Dns]::GetHostAddresses($ComputerName) #String hostNameOrAddress
$ping = new-object system.net.networkinformation.ping
#$reply = $ping.Send($IpAddress,$timeout,$buffer,$options) #Can't use $IpAddress. It's an array of IP4+IP6 of ALL adapters, Real and virtual.
$reply = $ping.Send($ComputerName,$timeout,$buffer,$options)
[String]$hostname = ([System.Net.Dns]::GetHostEntry($ComputerName)).hostname #might fail if RDNS is broken, so...
} Catch {
$reply = @{} # Create empty hash table
$reply.status = 'FailDnsLookup'
$ErrorMessage = "$($_.Exception.Message) $($_.Exception.InnerException)"
Write-Log -Message "$ErrorMessage" -Severity 3 -Source ${CmdletName}
}
If ($reply.status -eq "Success"){ $IsAlive = $true } else { $IsAlive = $false }
$info = @{} # Create empty hash table
$info.InputGiven = $ComputerName
$info.status = $reply.status #Success,TtlExpired,PacketTooBig
$info.RoundtripTime = $reply.RoundtripTime #In MilliSeconds
$info.Hostname = $hostname
$info.AddressUsed = $reply.Address
$info.AddressAll = $IpAddress #Ping $envComputerName to see multiple addresses (ip4/ip6)
$info.TimeToLive = $options.TTL
$info.DontFragment = $options.DontFragment
$info.IsAlive = $IsAlive
$info.Buffer = $buffer
$info.ErrorMessage = $ErrorMessage
If ($PassThru) {
New-Object PSObject -Property $info -ErrorAction SilentlyContinue
} Else {
Write-Output $IsAlive
}
}
Catch {
Write-Log -Message "Failed ping to see if [$ComputerName] is alive on the network: $($_.Exception.Message) $($_.Exception.InnerException)" -Severity 3 -Source ${CmdletName}
If ($PassThru) {
If ($info) { New-Object PSObject -Property $info -ErrorAction SilentlyContinue }
} Else {
Write-Output $false
}
}
}
End {
Write-FunctionHeaderOrFooter -CmdletName ${CmdletName} -Footer
}
}
#endregion Function Test-Ping