I have a int variable defined outside the foreach block, trying to set it within the block and access it outside. However, when I run this script, I get below error. It seems to be a basic thing but I am new to PowerShell.
Error : The '++' operator works only on variables or on properties.
Script :
[int]$ErrorCount = 0
1..3 | ForEach-Object -ThrottleLimit 40 -Parallel {
$using:ErrorCount++
}
$FinishMessage = "Errors: {0}" -f $ErrorCount
echo $FinishMessage
A more typical idiom would be to return some properties: