Start-DscConfiguration doesn't throw exceptions? Start-DscConfiguration doesn't throw exceptions? powershell powershell

Start-DscConfiguration doesn't throw exceptions?


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];}


There's another way to make it cause an exception. Try saving it into the ErrorVariable like this :

try{   Start-DscConfiguration -Path ".\MyConfig" -Wait -Verbose -ErrorVariable ev}catch{    $myException = $_}

Weirdly so, this throws the exception when there's an error (which is what you wanted). You can get the value of your exception in the $myexception variable, and also could get just a one liner description of your error using $ev

PS: Note that while mentioning ev in the errorVariable parameter, you do it without the '$' symbol - since you're only specifying the variable 'name'.


Start-DscConfiguration when used without -Wait will create a job object - with one child job for every computername. PowerShell job objects have an Error stream which contains all the errors. You can check this stream as well

$job = Start-DscConfiguration -Force -Verbose -Path C:\Temp\Demo\ -ComputerName localhost

Receive-Job $job -Wait

'Errors in job = ' + ($job.childjobs[0].Error.Count)