gpt4 book ai didi

powershell - Foreach 对象并行 block 中的错误处理 - Powershell 7

转载 作者:行者123 更新时间:2023-12-02 22:48:04 25 4
gpt4 key购买 nike

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

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

最佳答案

-ErrorVariable 参数将在末尾为您提供 ErrorRecords 的数组列表:

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

关于powershell - Foreach 对象并行 block 中的错误处理 - Powershell 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64239003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com