我有以下腳本,如何捕獲start-job的scriptblock中引發的異常?
$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
我更新了我的回答,以向您展示您之前的問題。 – 2012-01-05 23:05:51
目前還不清楚你在這份工作中想完成什麼。看起來好像你同時需要異步和同步行爲,這是不可能的。你什麼時候想要執行腳本中的下一行? – zdan 2012-01-06 01:03:39