Search code examples
powershellpingout-gridview

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.


Solution

  • 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.