对一个gridview输出运行多个测试连接 [英] Run multiple test-connection for one gridview output
问题描述
如何使用多个Test-Connection
cmdlet并将它们全部放在一个Out-GridView
中,或者我在这里尝试做的另一种解决方案?
关键是要能够一次又一次地对多个地址进行ping操作,并将它们全部显示在同一窗口中.
How can I use multiple Test-Connection
cmdlets and put them all in one Out-GridView
, or is there another solution to what I'm trying to do here?
The point is to be able to ping multiple adresses after one another and have it all appear in the same window.
推荐答案
将您的IP地址(或主机名)列表放入为每个地址运行Test-Connection
的ForEach-Object
循环中,然后将结果通过管道传送到Out-GridView
中:
Feed your list of IP addresses (or hostnames) into a ForEach-Object
loop running Test-Connection
for each address, then pipe the result into Out-GridView
:
$addr = '192.168.1.13', '192.168.23.42', ...
$addr | ForEach-Object {
Test-Connection $_
} | Out-GridView
请注意,这可能会非常耗时,具体取决于要检查的地址数,因为它们都是按顺序检查的.
Note that this can be quite time-consuming, depending on the number of addresses you're checking, because all of them are checked sequentially.
如果您需要加快大量地址的处理速度,则可以并行运行检查后台作业:
If you need to speed up processing for a large number of addresses you can for instance run the checks as parallel background jobs:
$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
但是,太多的并行性可能会耗尽您的系统资源.根据要检查的地址数量,您可能需要找到顺序运行和并行运行之间的中间地带,例如,使用
Too much parallelism might exhaust your system resources, though. Depending on how much addresses you want to check you may need to find some middle ground between running things sequentially and running them in parallel, for instance by using a job queue.
这篇关于对一个gridview输出运行多个测试连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!