Powershell 7 ->;将结果添加到数组时,函数中的 ForEach -Parallel 不会返回任何内容

发布于 2025-01-13 16:25:17 字数 937 浏览 1 评论 0原文

我需要在此函数中使用 Powershell 7 并行循环功能,但是当使用 ForEach 循环时,我无法获取结果并将其放入最后的数组中,我不明白为什么。

有什么想法吗?

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-01-20 16:25:17

这里的问题是并行运行的线程必须更改同一个数组。有一些方法可以实现这一点,但在这种情况下,只需从函数中发出 $myobject 就足够了。我删除了数组和将 $myObject 添加到数组的代码。

当调用该函数时,会自动创建数组,因此结果是相同的。

Function Get-ResponseFromParallelPings($activeHops) {
    $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 
        }
        $myObject
    }
}

The problem here is that threads running in parallel have to alter the same array. There are methods to make that happen, but in this case it is enough to just emit $myobject from the function. I removed the array and the code to add $myObject to the array.

When the function is called the array is created automatically, so the result is the same.

Function Get-ResponseFromParallelPings($activeHops) {
    $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 
        }
        $myObject
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文