Error Handling within Foreach-object Parallel - Powershell 7

Neelesh Rawal 21 Reputation points
2020-10-07T08:35:29.13+00:00

What would be the best way to capture error within below Foreach-Object Parallel block as there would be three separate threads/runspaces running and executing code written in the block and multiple errors/exception can occurs at the same time? Would it be possible to capture all the errors in a list/variable and show at the end of the execution of the script?

1..3 | ForEach-Object -ThrottleLimit 3 -Parallel { #Some code here that throws error }

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author
SChalakov 10,941 Reputation points MVP Volunteer Moderator
2020-10-07T08:43:08.883+00:00

Hi,
the approach, which you can follow is the same as in PowerShell parallel workflows. Here you can find details and also great examples:

PowerShell Time Sync: Error Handling in Parallel in Workflows

I hope I was able to help.


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Regards,
Stoyan

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.