Error handling within Foreach-object Parallel block - Powershell 7

泄露秘密 提交于 2021-01-27 12:34:58

问题


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                
}

回答1:


The -ErrorVariable param will give you an arraylist of ErrorRecords at the end:

1..3 | ForEach-Object -ThrottleLimit 3 -Parallel {
    throw "Item $_ Error"
} -ErrorVariable allErrors


来源:https://stackoverflow.com/questions/64239003/error-handling-within-foreach-object-parallel-block-powershell-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!