I noticed that if applying a configuration through Start-DscConfiguration fails, it writes to the error stream but doesn't
throw an Exception? That is, if I do the following:
try{
    Start-DscConfiguration -Path ".\MyConfig" -Wait -Verbose
}catch{
    #...
}
...it never ends up in the catch handler. I suspect this may have something to do with the fact that without the "-Wait",
Start-DscConfiguration starts an async job for this, and async commands probably don't throw exceptions, but in a synchronous
scenario, I would very much like to know if my configuration could be applied.
What is the proper way to determine if Start-DscConfiguration has completed succesfully?
The only way I know is to check the global "$error" variable and compare the number of error records before and after your call to Start-DscConfiguration. If there's more afterwards then something must have gone wrong during the call, so throw your own exception:
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];
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With