Powershell: Catch exception levée lorsqu'il est incapable de démarrer un service

Je semble incapable de rattraper une exception levée par Start-Service. Voici mon code:

try
{
    start-service "SomeUnStartableService"
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    write-host "got here"
}

Lorsque je l'exécute, l'exception est levée, mais n'est pas pris:

*Service 'SomeUnStartableService' start failed.
At line:3 char:18
+     start-service <<<<  "SomeUnStartableService"
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand*

$ErrorActionPreference est réglé sur Arrêt, donc cela ne devrait pas être le problème.

Quand j'ai modifier mon code pour catch [Exception], l'exception est interceptée et "a obtenu ici" est imprimé.

Ne start-service jeter un ServiceCommandException ou autre chose? Il semble qu'il est mais je ne peux pas l'attraper!

--- Edit ---

Idéalement, je pourrais écrire la suite, et de lever une exception si start-service ne jetez pas une exception, et seulement intercepter une exception levée par start-service:

try
{
    start-service "SomeUnStartableService"
    throw (new-object Exception("service started when expected not to start"))
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    write-host "got here"
}

OriginalL'auteur Jack | 2011-07-13