$g = @("127.0.0.1")*50
Function Ping-Wmi {
PROCESS {
$wmi = get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
if ($wmi.StatusCode -eq 0) { $_ }
}
}
Function Ping-Net {
Begin { $ping = new-object System.Net.NetworkInformation.Ping }
Process {
if ($ping.Send($_)) {
$_
}
}
}
Function Ping-Exe {
Process {
if ((Ping -n 1 $_|out-string) –match “Reply”) {
$_
}
}
}
PS C:\> measure-command { $g | Ping-Wmi }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 802
Ticks : 8023905
TotalDays : 9.28692708333333E-06
TotalHours : 0.00022288625
TotalMinutes : 0.013373175
TotalSeconds : 0.8023905
TotalMilliseconds : 802.3905
PS C:\> measure-command { $g | Ping-Net }
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 25
Ticks : 255965
TotalDays : 2.96255787037037E-07
TotalHours : 7.11013888888889E-06
TotalMinutes : 0.000426608333333333
TotalSeconds : 0.0255965
TotalMilliseconds : 25.5965
PS C:\> measure-command { $g | Ping-Exe }
Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 375
Ticks : 23757357
TotalDays : 2.74969409722222E-05
TotalHours : 0.000659926583333333
TotalMinutes : 0.039595595
TotalSeconds : 2.3757357
TotalMilliseconds : 2375.7357
从这个结果来看,Ping-Exe=2375.7357 > Ping-Wmi=802.3905 > Ping-Net=25.5965,该选择哪个就不用说了:)