简体   繁体   中英

Run multiple test-connection for one gridview output

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.

you can use this command :

$tests=  Test-Connection -ComputerName $env:COMPUTERNAME
$tests+= Test-Connection -ComputerName $env:COMPUTERNAME
$tests| 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM