Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception Powershell:无法启动服务时引发捕获异常_Exception_Service_Powershell_Try Catch - Fatal编程技术网

Exception Powershell:无法启动服务时引发捕获异常

Exception Powershell:无法启动服务时引发捕获异常,exception,service,powershell,try-catch,Exception,Service,Powershell,Try Catch,我似乎无法捕获启动服务引发的异常。这是我的密码: try { start-service "SomeUnStartableService" } catch [Microsoft.PowerShell.Commands.ServiceCommandException] { write-host "got here" } 运行此操作时,会引发异常,但不会捕获异常: *Service 'SomeUnStartableService' start failed. At line:3 c

我似乎无法捕获
启动服务
引发的异常。这是我的密码:

try
{
    start-service "SomeUnStartableService"
}
catch [Microsoft.PowerShell.Commands.ServiceCommandException]
{
    write-host "got here"
}
运行此操作时,会引发异常,但不会捕获异常:

*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*

Try/Catch仅适用于终止错误。使用值为Stop的ErrorAction参数将错误设置为终止错误,然后您将能够捕获它:

try
{
    start-service "SomeUnStartableService" -ErrorAction Stop
}
catch
{
    write-host "got here"
}
更新:

当您将$ErrorActionPreference设置为“stop”(或使用-ErrorAction stop)时,您得到的错误类型是ActionPreferenceStopException,因此您可以使用它捕获错误

$ErrorActionPreference='stop'

try
{
    start-service SomeUnStartableService
}
catch [System.Management.Automation.ActionPreferenceStopException]
{
    write-host "got here"
}

}要发现您的异常,您可以使用:

try
{
  start-service "SomeUnStartableService" -ea Stop
}
catch
{
 $_.exception.gettype().fullname
}

编辑:这是一种带有
SystemException

try
{
  start-service "SomeUnStartableService" -ea Stop
}
catch [SystemException]
{
 write-host "got here"
}

我通常不限制catch短语,而是在catch块中使用逻辑测试来处理异常:

try
{
  start-service "SomeUnStartableService" -ea Stop
}
catch
{
   if ( $error[0].Exception -match "Microsoft.PowerShell.Commands.ServiceCommandException")
   {
      #do this
   }
   else
   {
      #do that
   }
}

可能没有那么干净,可能会造成巨大的挡块。但如果它有效的话……;)

如果$ErrorActionPreference设置为Stop还不够?因为$ErrorActionPreference设置为'Stop',添加-erroraction Stop没有什么区别。上面的代码捕获了一个常规的[Exception],但是我只希望捕获启动服务抛出的异常,这意味着我可以在try块中抛出另一个异常,但不能在捕获中捕获它。很抱歉,错过了这一部分。我已经更新了线程,请检查。ActionPreferenceStopException确实是不直观的。谢谢你的解释!当我试图捕获我真正想要捕获的异常时,我以为PowserShell只是忽略了$ErrorActionPreference值。这将返回Microsoft.PowerShell.Commands.ServiceCommandException,这正是我在上面尝试捕获的。编辑确实有帮助,而且我认为符合目的,所以我投票支持它,但我不认为这是一个答案,因为这是一个有点变通的方法。遗憾的是,它没有捕捉到特定的异常那么整洁,但我想如果不能做到这一点,这是下一个最好的方法。
try
{
  start-service "SomeUnStartableService" -ea Stop
}
catch
{
   if ( $error[0].Exception -match "Microsoft.PowerShell.Commands.ServiceCommandException")
   {
      #do this
   }
   else
   {
      #do that
   }
}