3

在 Foreach-Object Parallel 块下捕获错误的最佳方法是什么,因为将有三个单独的线程/运行空间运行和执行块中编写的代码,并且可能同时发生多个错误/异常?是否可以捕获列表/变量中的所有错误并在脚本执行结束时显示?

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

1 回答 1

0

-ErrorVariable 参数最后会给你一个 ErrorRecords 的数组列表:

1..3 | ForEach-Object -ThrottleLimit 3 -Parallel {
    throw "Item $_ Error"
} -ErrorVariable allErrors
于 2020-10-10T01:19:06.830 回答