I just did a Wireshark trace while running Test-Connection
with and without the Quiet
switch parameter, supplying an IPv4 address for the Computername
parameter.
When the Quiet
switch is omitted, PowerShell seems to send not 1, but 6 NetBIOS Name Queries to the target machine, after which it returns the formatted output.
If I assign the output from Test-Connection
, it returns right away, but as soon as I pipe it to Format-Table
, it hangs and sends the NBSTAT queries again
The root cause is actually not the Test-Connection
cmdlet itself, but the formatted output. One of the properties (IPV4Address
) is a ScriptProperty
and has the following definition:
PS C:> $ping = Test-Connection -ComputerName 10.0.0.101 -Count 1
PS C:> Get-Member -InputObject $ping -Name IPV4Address | Select-Object -ExpandProperty Definition
System.Object IPV4Address {get=$iphost = [System.Net.Dns]::GetHostEntry($this.address)
$iphost.AddressList | ?{ $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork } | select -first 1;}
So, when the output shown, [System.Net.Dns]::GetHostEntry(10.0.0.101)
is called to calculate IPV4Address
- this is what causes the waiting time
If you don't care about the moot resolution of the IP address, use Select-Object
to prevent the calculation and output of IPV4Address
:
Test-Connection -ComputerName 10.0.0.101 -Count 1 | Select Address,StatusCode
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…