I need to use Powershell 7 Parallel looping feature in this function but when using ForEach loop, I cannot take the result and put it into the array at the end and I do not undestand why.
Any ideas?
Function Get-ResponseFromParallelPings($activeHops) {
$ArrayOfObjects = @()
$activeHops | ForEach-Object -Parallel {
$count = 5
$LatencyNumber = 0
$SuccessNumber = 0
$Answer = Test-Connection -count $count -targetname $_.Name -delay 1
foreach ($a in $Answer) {
$LatencyNumber += $a.Latency / $count
if ($a.Status -eq "Success") {
$IncreaseBy = 100 / $count
$SuccessNumber += $IncreaseBy
}
}
$myObject = [PSCustomObject]@{
DestinationIP = $_.Name
AverageLatency = $LatencyNumber
Success = $SuccessNumber
}
$arrayOfObjects += $myObject # <- This line does not work for me.
}
return $arrayOfObjects
}