【发布时间】:2020-04-22 21:22:04
【问题描述】:
我有以下脚本,
$createZip = {
Param ([String]$source, [String]$zipfile)
Process {
echo "zip: $source`n --> $zipfile"
throw "test"
}
}
try {
Start-Job -ScriptBlock $createZip -ArgumentList "abd", "acd"
echo "**Don't reach here if error**"
LogThezippedFile
}
catch {
echo "Captured: "
$_ | fl * -force
}
Get-Job | Wait-Job
Get-Job | receive-job
Get-Job | Remove-Job
但是,无法捕获另一个 powershell 实例中引发的异常。捕获异常的最佳方法是什么?
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
343 Job343 Running True localhost ...
**Don't reach here if error**
343 Job343 Failed True localhost ...
zip: abd
--> acd
Receive-Job : test
At line:18 char:22
+ Get-Job | receive-job <<<<
+ CategoryInfo : OperationStopped: (test:String) [Receive-Job], RuntimeException
+ FullyQualifiedErrorId : test
【问题讨论】:
-
我更新了我的答案,以向您展示您上一个问题中的方法。
-
不清楚你想通过这份工作完成什么。似乎您同时想要异步和同步行为,这是不可能的。您希望何时执行脚本中的下一行?