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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

Accepted answer
  1. SChalakov 10,261 Reputation points MVP
    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

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful