Run multiple test-connection for one gridview output

风格不统一 提交于 2019-12-25 08:26:06

问题


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.


回答1:


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.




回答2:


you can use this command :

$tests=  Test-Connection -ComputerName $env:COMPUTERNAME
$tests+= Test-Connection -ComputerName $env:COMPUTERNAME
$tests| Out-GridView



回答3:


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.



来源:https://stackoverflow.com/questions/41740833/run-multiple-test-connection-for-one-gridview-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!