Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Multithread PowerShell Ping Script

I recently finished a script to ping every computer/workstation on a list and output it in a nice format.

There are thousands of computers to ping on the next list so it would take a while to run normally. How might I be able to multithread this so the job can be completed within a reasonable time-frame?

like image 439
MZIP Avatar asked Mar 12 '23 22:03

MZIP


2 Answers

workflow Ping
{
  param($computers)

    foreach -parallel ($computer in $computers)
    {
        $status = Test-Connection -ComputerName $computer -Count 1 -Quiet

        if (!$status)
            { Write-Output "Could not ping $computer" }
    }
}

$computers = @(
    "wd1600023",
    "sleipnir"
)

Ping $computers
like image 170
David Brabant Avatar answered Mar 20 '23 23:03

David Brabant


You could spawn multiple jobs or you could use a workflow and a Foreach -parallel loop.

like image 38
Martin Brandl Avatar answered Mar 20 '23 23:03

Martin Brandl