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.
This topic is 3 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 318
Last visit: Sat Oct 28, 2023 7:46 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 318
Last visit: Sat Oct 28, 2023 7:46 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: 3
Last visit: Fri Dec 11, 2020 4:14 am

Re: Calling function in a Parallel foreach loop

Post by JackMayhoffer »

sekou2331 wrote: Sat Nov 07, 2020 4: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: 318
Last visit: Sat Oct 28, 2023 7:46 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 318
Last visit: Sat Oct 28, 2023 7:46 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Correct. The same is true for run spaces and jobs.
This topic is 3 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked