![](/img/trans.png)
[英]Error handling within Foreach-object Parallel block - Powershell 7
[英]How to set a counter within ForEach-Object Parallel - PowerShell 7
我在 foreach 块之外定义了一个 int 变量,试图将它设置在块内并在外部访问它。 但是,当我运行此脚本时,出现以下错误。 这似乎是一件基本的事情,但我是 PowerShell 的新手。
错误: “++”运算符仅适用于变量或属性。
脚本:
[int]$ErrorCount = 0
1..3 | ForEach-Object -ThrottleLimit 40 -Parallel {
$using:ErrorCount++
}
$FinishMessage = "Errors: {0}" -f $ErrorCount
echo $FinishMessage
一个更典型的习惯用法是返回一些属性:
1..40 | ForEach-Object -ThrottleLimit 40 -Parallel {
sleep 1
[pscustomobject]@{Error = $true }
} | measure error
Count : 40
Average :
Sum :
Maximum :
Minimum :
StandardDeviation :
Property : Error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.