次のスクリプトがあります。
$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
throw
を使用すると、ジョブオブジェクトのState
プロパティが「失敗」に変更されます。重要なのは、Start-Job
またはGet-Job
から返されたジョブオブジェクトを使用し、State
プロパティを確認することです。その後、ジョブオブジェクト自体から例外メッセージにアクセスできます。
リクエストに応じて、同時実行性も含めるように例を更新しました。
$createZip = {
Param ( [String] $source, [String] $zipfile )
if ($source -eq "b") {
throw "Failed to create $zipfile"
} else {
return "Successfully created $zipfile"
}
}
$jobs = @()
$sources = "a", "b", "c"
foreach ($source in $sources) {
$jobs += Start-Job -ScriptBlock $createZip -ArgumentList $source, "${source}.Zip"
}
Wait-Job -Job $jobs | Out-Null
foreach ($job in $jobs) {
if ($job.State -eq 'Failed') {
Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
} else {
Write-Host (Receive-Job $job) -ForegroundColor Green
}
}
これは本当にコメントであるべきですが、コメントを残すという評判はありません。
私の答えは、Andy Arismendiの答えを使用する必要がありますが、出力$job.ChildJobs[0].Error
なので $job.ChildJobs[0].JobStateInfo.Reason.Message
が常に役立つとは限りません。
私はメインスレッドで例外を「再スロー」することができました:
Receive-Job $job -ErrorAction Stop
例として使用例を示します。 OPに簡単に適用できます。
$code = {
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
#Errors from Search are not terminating, but will be present in the output none the less.
$Results = $Searcher.Search('IsInstalled=0 and IsHidden=0')
$Results.Updates
};
$job = Start-Job -ScriptBlock $code;
$consume = Wait-Job $job -Timeout 600;
if ($job.state -eq 'Running') {
Stop-Job $job
throw 'Windows update searcher took more than 10 minutes. Aborting'
};
#Captures and throws any exception in the job output
Receive-Job $job -ErrorAction Stop;
Write-Host "Finished with no errors"; #this will not print if there was an error
V2.0で動作します。
ジョブ内のエラーが終了しない場合、後続の行は引き続き実行されることに注意してください。ただし、Receive-Jobは「途中で終了する」ため、Receive-Jobから返される出力ではこれは明らかではありません。エラーオブジェクトが検出されると、それ自体がスローされます。
これを回避する1つの方法は、ブロック全体をtry {} catch {throw;}でラップすることです。
また、例外が終了しない場合、ジョブの状態は「失敗」にはなりません