如何使用多个Test-Connection
cmdlet 并将它们全部放在一个中Out-GridView
,或者是否有另一种解决方案来解决我在这里尝试做的事情?关键是能够一个接一个地 ping 多个地址,并将其全部显示在同一个窗口中。
3 回答
你可以使用这个命令:
$tests= Test-Connection -ComputerName $env:COMPUTERNAME
$tests+= Test-Connection -ComputerName $env:COMPUTERNAME
$tests| Out-GridView
将您的 IP 地址(或主机名)列表输入为每个地址ForEach-Object
运行的循环Test-Connection
中,然后将结果通过管道传输到Out-GridView
:
$addr = '192.168.1.13', '192.168.23.42', ...
$addr | ForEach-Object {
Test-Connection $_
} | Out-GridView
请注意,这可能非常耗时,具体取决于您要检查的地址数量,因为它们都是按顺序检查的。
如果您需要加快处理大量地址的速度,例如可以将检查作为并行后台作业运行:
$addr | ForEach-Object {
Start-Job -ScriptBlock { Test-Connection $args[0] } -ArgumentList $_
} | Out-Null
$results = do {
$running = Get-Job -State Running
Get-Job -State Completed | ForEach-Object {
Receive-Job -Job $_
Remove-Job -Job $_
}
} while ($running)
$results | Out-GridView
但是,过多的并行性可能会耗尽您的系统资源。根据您要检查的地址数量,您可能需要在顺序运行和并行运行之间找到一些中间立场,例如使用作业队列。
Test-Connection
can take a array of computer names or addresses and ping them. It will return a line for each ping on each computer but you can use the -Count
parameter to restrict it to 1 ping. You can also use the -AsJob
to run the command as a background job.
$names = server1,server2,serverN
Test-Connection -ComputerName $names -Count 1 -AsJob | Wait-Job | Receive-Job
You will get back a list of Win32_PingStatus object that are show as
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
If the time column (ResponseTime property) is empty, there is no ping replay, the server is offline. You can filter on this.