Calling function in a Parallel foreach loop

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
sekou2331
Posts: 295
Joined: Wed Aug 15, 2012 6:07 am

Calling function in a Parallel foreach loop

Post by sekou2331 »

Wanted to know if I created 3 function and wanted to call them in a foreach Parallel loop is this possible? I have been searching and I dont see anyone trying to do this.

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

What code have you tried? There is no reason why you can't do this.

User avatar
sekou2331
Posts: 295
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

Cant seem to find any documentation on calling outside functions. Will write something and see if I can get it to work.

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Code: Select all

1..10 | 
    Foreach-Object -Parallel {
        function test1{
            param($P1)
            Write-Host "Function One says $P1"
        }
        function test2{
            param($P1)
            Write-Host "Function Two says $(2 * $P1)"
        }
        test1 $_
        test2 $_
    }

User avatar
sekou2331
Posts: 295
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

Ok please dont close post I am going to write the script and see if is works.

User avatar
JackMayhoffer
Posts: 2
Joined: Fri Sep 05, 2014 11:19 am

Re: Calling function in a Parallel foreach loop

Post by JackMayhoffer »

sekou2331 wrote:
Sat Nov 07, 2020 5:49 pm
Ok please dont close post I am going to write the script and see if is works.
Any progress there?

User avatar
sekou2331
Posts: 295
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

So it looks like this is for PowerShell 7. I am on PowerShell 5. Can this be done with workflows? If so is it possible to use work flow with function?

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Yes. You can only use "ForEach -Parallel" with PS 7.

Workflows can contain functions and load modules.

User avatar
sekou2331
Posts: 295
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

But the function would have to be in the the workflow and can not be called from outside the workflow?

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Correct. The same is true for run spaces and jobs.

Post Reply