Feb 24 2022 01:44 AM - edited Feb 24 2022 02:09 AM
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.
Feb 24 2022 01:55 AM
Feb 24 2022 02:09 AM - edited Feb 24 2022 02:13 AM
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 2022 02:13 AM
Feb 24 2022 02:18 AM
Feb 24 2022 02:27 AM
Feb 24 2022 02:37 AM
Feb 24 2022 02:49 AM
$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 02:58 AM - edited Feb 24 2022 03:09 AM
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-...
In your case $using:_.Description I guess for example? Not sure how to format that in this case...
Feb 24 2022 03:47 AM
"Parallel running script block. Beginning with PowerShell 7.0, a third parameter set is available that runs each script block in parallel. The ThrottleLimit parameter limits the number of parallel scripts running at a time. As before, use the $_ variable to represent the current input object in the script block. Use the $using: keyword to pass variable references to the running script."
Then https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=pow...
I don't think it's gonna work and I don't see how I would use it.
Also, https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=pow...
This suggest that my $_. should work.
I think I'm lost in translation 😄
Feb 24 2022 03:57 AM
Feb 24 2022 05:35 AM
Feb 24 2022 06:11 AM