问题
I have an array of objects with properties "IP" for storing an ip address and "LastAlive" for storing the date that the server last responded to a ping.
In order to fill the LastAlive properties I use the following function:
function Update-AliveStatus ($nodes)
{
foreach ($node in $nodes)
{
$alive = Test-Connection $node.IP -Quiet -Count 2
if ($alive -eq $true)
{
$node.LastAlive = (Get-Date)
}
}
return $nodes
}
This works fine unless the list of IPs is long, in which case it takes ages to iterate through all of them. How can I modify the function to run the pings asynchronously and still return the filled array when all the pings are done?
来源:https://stackoverflow.com/questions/24930714/pinging-an-array-of-ip-addresses-concurrently-in-powershell