并行Ping数千台计算机,将结果保存到阵列

时间:2019-01-09 21:01:49

标签: powershell active-directory ping

我有以下代码。我想让我的pingAll函数调用ping工作流,以便获得所有在线计算机的列表。由于要查询的计算机数量众多,因此必须并行执行此操作。

唯一的问题是我无法在工作流中调用$alive.Add($computer),因为“ Windows Powershell工作流不支持方法调用”。

既然是这种情况,如何将来自foreach的结果保存到某种数组或数据结构中?

function pingAll
{    
    $computers = Get-ADComputer -filter * -Properties CN | select CN
    [System.Collections.ArrayList]$alive = @() #array of online machines
}
workflow ping ($computers)
{
    foreach -parallel ($computer in $computers)
    {
        if (Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {$alive.Add($computer) }
    }
}

0 个答案:

没有答案