Start-DscConfiguration不会抛出异常?

Ast*_*ain 5 powershell dsc

我注意到如果通过Start-DscConfiguration失败应用配置,它会写入错误流但不会抛出异常?也就是说,如果我执行以下操作:

try{
    Start-DscConfiguration -Path ".\MyConfig" -Wait -Verbose
}catch{
    #...
}
Run Code Online (Sandbox Code Playgroud)

......它永远不会在catch处理程序中结束.我怀疑这可能与以下事实有关:没有"-Wait", Start-DscConfiguration为此启动异步作业,异步命令可能不会抛出异常,但在同步场景中,我非常想知道我的配置是否可以应用.

确定是否Start-DscConfiguration已成功完成的正确方法是什么?

mcl*_*ton 5

我知道的唯一方法是检查全局"$ error"变量并比较调用Start-DscConfiguration之前和之后的错误记录数.如果事后有更多事情,那么在通话过程中肯定会出错,所以抛出你自己的异常:

Configuration TestErrorHandling {
    Node "localhost" {
        Script ErroringResource {
            GetScript =  { return $null; }
            TestScript = { return $false; }
            SetScript = { throw new-object System.InvalidOperationException; }
        }
    }
}

$errorCount = $error.Count;

write-host "starting dsc configuration"
$mof = TestErrorHandling;
Start-DscConfiguration TestErrorHandling –Wait –Verbose;
write-host "dsc configuration finished"

if( $error.Count -gt $errorCount )
{
    $dscErrors = $error[$errorCount..($error.Count - 1)];
    write-host "the following errors occurred during dsc configuration";
    write-host ($dscErrors | fl * | out-string);
    throw $dscErrors[-1];
}
Run Code Online (Sandbox Code Playgroud)