如何多线程 PowerShell Ping 脚本

MZI*_*ZIP 6 powershell multithreading ping

我最近完成了一个脚本,用于 ping 列表中的每台计算机/工作站并以良好的格式输出。

有数千台计算机需要 ping 下一个列表,因此需要一段时间才能正常运行。我如何能够对其进行多线程处理,以便可以在合理的时间范围内完成这项工作?

Dav*_*ant 6

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
Run Code Online (Sandbox Code Playgroud)


Mar*_*ndl 3

您可以生成多个作业 ,也可以使用工作流程Foreach 并行循环