如何捕获start-job的scriptblock中引发的异常?

时间:2012-01-05 23:03:58

标签: powershell powershell-v2.0

我有以下脚本,

$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

4 个答案:

答案 0 :(得分:23)

使用throw会将作业对象的State属性更改为“失败”。关键是使用从Start-JobGet-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 
    }
}

答案 1 :(得分:8)

这应该是一个评论,但我没有留下评论的声誉。

我的回答是你应该使用Andy Arismendi的答案,但也输出$job.ChildJobs[0].Error

由于$job.ChildJobs[0].JobStateInfo.Reason.Message并非总是有用。

答案 2 :(得分:2)

我能够&#34;重新抛出&#34;主线程中的异常使用:

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&#34;在一半时间内终止&#34; - 当遇到错误对象时,它会抛出它的自我。

避免这种情况的一种方法是将整个块包装在try {} catch {throw;}

此外,工作状态不会失败&#39;如果异常是非终止的

答案 3 :(得分:0)

TLDR:

# Works with both terminating and non terminating errors
$j = start-job {1/0} | wait-job; try { rcjb $j -ErrorAction Stop } catch { "err $_" }