【问题标题】:Powershell: Catch exception thrown when unable to start a servicePowershell:捕获无法启动服务时引发的异常
【发布时间】:2011-10-04 07:47:51
【问题描述】:

我似乎无法捕获Start-Service 引发的异常。这是我的代码:

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*

$ErrorActionPreference 设置为停止,所以这应该不是问题。

当我将代码更改为 catch [Exception] 时,异常被捕获并打印“到这里”。

start-service 是否会抛出 ServiceCommandException 或其他内容?好像是,但我抓不住!

--- 编辑---

理想情况下,我可以编写以下代码,如果start-service 没有抛出异常,则抛出异常,并且只捕获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"
}

【问题讨论】:

    标签: exception service powershell try-catch


    【解决方案1】:

    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"
    }
    

    }

    【讨论】:

    • 如果 $ErrorActionPreference 设置为 Stop 还不够?
    • 由于 $ErrorActionPreference 设置为“Stop”,因此添加 -erroraction Stop 没有区别。上面的代码捕获了一个一般的 [Exception],但是我希望只捕获 start-service 抛出的异常,这意味着我可以在 try 块中抛出另一个异常,但不会在 catch 中捕获它。
    • 对不起,错过了那部分。我已经更新了主题,请查看。
    • ActionPreferenceStopException真的不直观。感谢您的解释!当我试图捕获我真正想要捕获的异常时,我认为 PowserShell 只是忽略了 $ErrorActionPreference 值。
    【解决方案2】:

    我通常不限制 catch-phrase,而是通过 catch-block 中的逻辑测试来处理异常:

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

    可能不那么干净,并且可能导致巨大的捕获块。但如果它有效...... ;)

    【讨论】:

    • 很遗憾,它不如捕捉特定异常那么整洁,但我想如果不能做到这一点,这是下一个最好的事情。
    【解决方案3】:

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

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

    已编辑:这是SystemException的一种绕过

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

    【讨论】:

    • 这会返回 Microsoft.PowerShell.Commands.ServiceCommandException,这是我在上面试图捕捉到的。
    • 编辑确实有帮助,我认为符合目的,所以我投了赞成票,但我认为这不是答案,因为它是一种解决方法。
    猜你喜欢
    • 2013-09-01
    • 2013-02-26
    • 2013-12-14
    • 1970-01-01
    • 2021-10-25
    • 2013-07-23
    • 1970-01-01
    • 2015-04-19
    • 2023-03-09
    相关资源
    最近更新 更多