Forum Discussion
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.ArrayList]$ArrayWithHeader = @()
$Date = Get-date -Format "yyyy_MM_dd HH-mm"
$ListOldOSesPath = ".\ListOldOSes\$date"
#Check backup folder
if (-Not(Test-Path -Path $ListOldOSesPath)) {New-Item -ItemType Directory $ListOldOSesPath -Force }
$complist = Get-ADComputer -Filter * -Property OperatingSystem,name,enabled,Description,distinguishedname #| Where-Object -Property name -EQ "SRVCOPWDC02P"
$total = $complist.count
$count = 1
#Workflow TestParallel{
$complist | ForEach-Object -parallel{
#foreach ($comp in $complist){
$PC = $_.name
write-host "working on $PC $count/$total"
#write-host "working on $_ "
$pingtest = Test-Connection -ComputerName $PC -Quiet -Count 1 -ErrorAction SilentlyContinue
#$Result = [pscustomobject]@{'OS'=$($_.OperatingSystem);'Computer_Name'=$($_.name);'DN'=$($_.distinguishedname);'enabled'=$($_.enabled);'Description'=$($_.Description);'Reacheable'=$($pingtest)}
$ArrayWithHeader.add([pscustomobject]@{'OS'=$_.OperatingSystem;'Computer_Name'=$_.name;'DN'=$_.distinguishedname;'enabled'=$_.enabled;'Description'=$_.Description;'Reacheable'=$pingtest}) | Out-Null
#$ArrayWithHeader.add($Result) | Out-Null
#$Result=$null
$count += 1
}
#}
#TestParallel
$ArrayWithHeader | export-csv -path "$ListOldOSesPath\_ListOldOSes.csv" -NoTypeInformation -Append -delimiter ";"
Invoke-Item "$ListOldOSesPath"
#Show script processing time
$TimeElapsed = $TimeDuration.elapsed.ToString('dd\.hh\:mm\:ss')
$TimeDuration.stop
$TimeElapsed
pause
ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At XXXXXXXXXXXXXX\ListOldOSes-OtherVersion.ps1:15 char:13
+ $complist | ForEach-Object -parallel{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [ForEach-Object], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand
I'm using PS 7.
Thank you.
12 Replies
- 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- John_DodoBrass 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.- And your $complist and $pingtest variables are filled?