Start-DscConfiguration does not throw exceptions?

I noticed that if the use of the configuration using Start-DscConfigurationfails, it is written to the error stream, but not throw an exception? That is, if I do the following:

try{
    Start-DscConfiguration -Path ".\MyConfig" -Wait -Verbose
}catch{
    #...
}

... it never gets into the catch handler. I suspect it may have something to do with the fact that no "-Wait", Start-DscConfigurationstarts for this async task and async team probably will not generate exceptions, but in the synchronous scenario, I really would like to know whether you can use my configuration.

What is the correct way to determine success Start-DscConfiguration?

+4
source share
3 answers

, , - "$ 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];
}
+5

. ErrorVariable :

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

, , ( ). $myexception, liner , $ev

PS: , , ev errorVariable, '$', 'name'.

+1

Start-DscConfiguration -Wait - . PowerShell , .

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

-Job $job -Wait

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

0

All Articles