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...
John_Dodo
Feb 24, 2022Brass Contributor
Normally $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.
[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, 2022
You 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'}
if ($Null -eq $_.Description) {$_.Description='Empty'}
- John_DodoFeb 24, 2022Brass ContributorI just modified my script to only test on 1 computer. I made sure this computer has all values filled.
Still, without the -parallel it works but with it it fails. Could it be that I can't use $_. with -parralel, from what I saw it should be feasible.
Details :
$complist = Get-ADComputer -Filter * -Property OperatingSystem,name,enabled,Description,distinguishedname | Select-Object OperatingSystem,name,enabled,Description,distinguishedname | Where-Object -Property name -EQ "PCNAME"
F8 on $complist :
[DBG]: PS C:\Users\bodig-adm> $complist
OperatingSystem : Windows Server 2008 R2 Standard
name : PCNAME
enabled : True
Description : UVS - File & Print
distinguishedname : CN=PCNAME,OU=CH - Servers,OU=CH,OU=XXX,DC=XXX,DC=XXX
So the properties have value.
Then foreach-object works but -parrallel fails
8 | $ArrayWithHeader.add([pscustomobject]@{'OS'=$_.OperatingS …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| You cannot call a method on a null-valued expression.
I will try to use intermediate variables...- John_DodoFeb 24, 2022Brass Contributor
$complist | ForEach-Object { $PC = $_.name $OS = $_.OperatingSystem $DN = $_.distinguishedname $enable = $_.enabled $desc = $_.Description Write-Host "test" write-host "working on $PC, $OS, $DN, $Enable, $desc, $count/$total" $pingtest = Test-Connection -ComputerName $PC -Quiet -Count 1 -ErrorAction SilentlyContinue write-host "$pingtest" #$ArrayWithHeader.add([pscustomobject]@{'OS'=$_.OperatingSystem;'Computer_Name'=$_.name;'DN'=$_.distinguishedname;'enabled'=$_.enabled;'Description'=$_.Description;'Reacheable'=$pingtest}) $tutu = [pscustomobject]@{'OS'=$OS;'Computer_Name'=$PC;'DN'=$DN;'enabled'=$enable;'Description'=$desc;'Reacheable'=$pingtest} Write-Host $tutu $ArrayWithHeader.add($tutu) $count += 1 }In both cases my variables are filled, but -parrallel gives an error. I know that in PS5 I had an error when trying to use ArrayWithHeader.add($tutu) (though it was with foreach -parrallel (not foreachobject)
- Feb 24, 2022
Put $using:sheet inside the loop, like with jobs and invoke-command.
https://stackoverflow.com/questions/63502924/error-while-executing-in-powershell7-0-you-cannot-call-a-method-on-a-null-value
In your case $using:_.Description I guess for example? Not sure how to format that in this case...