0

I'm currently trying to multi-thread a powershell (7) script. I have a lot of function that iterates over lists and i wanted to use -Parallel to go through them.

The problem is that inside the loop i call other functions to retrieve data for each object. I know that the loop create a process in a seperate thread (thus without the functions).

So i was wondering if there is a workaround for that, do i need to redefine all my function inside $code = @'code here' variables ?

My code is :

        $Results | Where-Object {$_} | ForEach-Object -Parallel {
            $Computer = $_
            $Up = $True
            if ($PSBoundParameters['Ping']) {
                $Up = Test-Connection -Count 1 -Quiet -ComputerName $_.properties.dnshostname
            }
            if ($Up) {
                if ($PSBoundParameters['Raw']) {
                    # return raw result objects
                    $Computer = $_
                    $Computer.PSObject.TypeNames.Insert(0, 'func.Computer.Raw')
                }
                else {
                    
                    $Computer = Convert-LDAPProperty -Properties $_.Properties
                    $Computer.PSObject.TypeNames.Insert(0, 'func.Computer')
                }
                $Computer
            }
        }

Error :

The loop is trying to access the function : Convert-LDAPProperty And when i execute my code i get the following error : InvalidOperation:

$Computer.PSObject.TypeNames.Insert(0, 'func.Compute … 
You cannot call a method on a null-valued expression.       
Convert-LDAPProperty: 

$Computer = Convert-LDAPProperty -Properties  …
The term 'Convert-LDAPProperty' is not recognized as a name 
of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Thank you for your time !

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Quentin_otd
  • 233
  • 1
  • 3
  • 16
  • 1
    You might want to check [How to create a delegate in powershell](https://stackoverflow.com/questions/19849848/powershell-how-to-create-a-delegate) – JonasH Jun 12 '23 at 08:17
  • I will check that, thank you for the documentation – Quentin_otd Jun 12 '23 at 08:21
  • I can’t see any calls to functions inside your ```foreach-object``` other than the built-in ```Test-Connection```. Can you show a representative sample that demonstrates your problem, and any error message you’re getting? – mclayton Jun 12 '23 at 08:32
  • Sure thing , i will detail my code in the main post asap – Quentin_otd Jun 12 '23 at 08:42
  • I would recommend creating a workflow function and run your code inside that workflow function. – SavindraSingh Jun 12 '23 at 12:41
  • Multi-threading using workflows is apprently slower, that's why i'm using For-EachObject -Parallel which is in powershell 7 only – Quentin_otd Jun 12 '23 at 13:20
  • 1
    Does this answer your question? https://stackoverflow.com/questions/61273189/how-to-pass-a-custom-function-inside-a-foreach-object-parallel. For reference, "I know that the loop create a process in a seperate thread (thus without the functions).", `ForEach-Object -Parallel` creates threads in the same process not in a separated one. They just have a fresh session state thus, everything defined in your current session is not seen by the threads – Santiago Squarzon Jun 12 '23 at 13:26
  • @SantiagoSquarzon; I will try the method in this post, thanks ! – Quentin_otd Jun 12 '23 at 13:40
  • @SantiagoSquarzon , it worked ! if you want you can post a real answer so i can mark it as answered :) – Quentin_otd Jun 12 '23 at 14:10
  • Thats fine we can close this one as a duplicate – Santiago Squarzon Jun 12 '23 at 14:13

0 Answers0