Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start-DscConfiguration doesn't throw exceptions?

Tags:

powershell

dsc

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?

like image 683
Leon Bouquiet Avatar asked Nov 29 '14 10:11

Leon Bouquiet


1 Answers

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];
}
like image 130
mclayton Avatar answered Nov 30 '22 13:11

mclayton