Forum Discussion
John_Dodo
Feb 24, 2022Brass Contributor
AmbiguousParameterSet with ForEach-Object -parallel
Hello, I have this error when trying to use parallele tasks. Can anybody help on this please? $TimeDuration = [System.Diagnostics.Stopwatch]::StartNew()
[System.Collections.ArrayLi...
Feb 24, 2022
Are you running the script in a PowerShell ISE editor perhaps?
"I ran into this issue when trying to test the script in the Windows Powershell ISE script editor, despite pwsh --version telling me that it was 7.2.0.
As Owain Esau points out in the comments, the error is telling you that the current version of ForEach-Object does not support the -Parallel parameter.
But if you open run your script in a dedicated Powershell 7 window, it should execute just fine."
https://stackoverflow.com/questions/62379047/foreach-object-parallel-parameter-set-cannot-be-resolved
"I ran into this issue when trying to test the script in the Windows Powershell ISE script editor, despite pwsh --version telling me that it was 7.2.0.
As Owain Esau points out in the comments, the error is telling you that the current version of ForEach-Object does not support the -Parallel parameter.
But if you open run your script in a dedicated Powershell 7 window, it should execute just fine."
https://stackoverflow.com/questions/62379047/foreach-object-parallel-parameter-set-cannot-be-resolved
John_Dodo
Feb 24, 2022Brass Contributor
Thank you !!!
I think it was indeed the answer. I use now Visual Studio and PS7 and this error is gone.
I now have an other issue.
InvalidOperation:
Line |
11 | $ArrayWithHeader.add([pscustomobject]@{'OS'=$_.OperatingS …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| You cannot call a method on a null-valued expression.
The concerned line is like this :
$ArrayWithHeader.add([pscustomobject]@{'OS'=$_.OperatingSystem;'Computer_Name'=$_.name;'DN'=$_.distinguishedname;'enabled'=$_.enabled;'Description'=$_.Description;'Reacheable'=$pingtest}) | Out-Null
I was doing something very similar in PS v5 with a foreach ($var in $...) and it was working fine, though very slow.
Also, if I remove the -parallel then it's working again.
- Feb 24, 2022And your $complist and $pingtest variables are filled?
- John_DodoFeb 24, 2022Brass ContributorNormally $pingtest always is at true or false
[DBG]: PS C:\Users\xxx> $pingtest
True
Regarding $complist it is indeed filled. BUT, sometimes one of the property can be empty sometimes.
[DBG]: PS C:\Users\xxxx> $complist
Description : WS2016 - DC
DistinguishedName : CN=XXX,OU=Domain Controllers,DC=unich,DC=net
DNSHostName : XXX
Enabled : True
Name : XXX
ObjectClass : computer
ObjectGUID : 2b85de89-1886-409c-92e0-1c7a97a6be48
OperatingSystem :
SamAccountName : XXX$
SID : S-1-5-21-1128282684-3729639265-1475195819-26153
UserPrincipalName :
Could that explain the problem ? Again without the -parallel the values of pingtest and the emptiness of properties is still the same, but seems to work.- Feb 24, 2022You could put in a check for the values before adding them to the array. Certain values are always present in AD but things like Description is a free text field. Perhaps something like
if ($Null -eq $_.Description) {$_.Description='Empty'}