powershell - Write-Error 在类方法内部使用时不会产生有用的信息 |电源外壳

标签 powershell class error-handling write-error

我使用了带类和不带类的方法,Write-Error 似乎产生了不同的输出。对于类,它不指定函数并且行号始终为 1,1

function oper1() {
    Try {
        [string] $cmd = ".\some_exe_which_does_not_exist.exe"
        iex $cmd 
    }
    Catch {
        Write-Error $_.Exception.Message
    }
}

oper1

上面的输出:

oper1 : The term '.\some_exe_which_does_not_exist.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At F:\debug\encryption_concat_tests\Untitled1.ps1:11 char:1 + oper1 + ~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,oper1

当我在一个类中包含相同的函数时,我得到了这个:

class Operator {
    [void] oper1() {
        Try {
            [string] $cmd = ".\some_exe_which_does_not_exist.exe"
            iex $cmd 
        }
        Catch {
            Write-Error $_.Exception.Message
        }
    }
}

[Operator] $operator = New-Object Operator
$operator.oper1()

The term '.\some_exe_which_does_not_exist.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + F:\debug\encryption_concat_tests\Untitled1.ps1 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

类内方法出现这种行为的原因可能是什么?

最佳答案

顺便说一句:Invoke-Expression (iex) should generally be avoided ;绝对don't use it to invoke an external program - 只需直接调用它,如下所示。


在 PowerShell 类方法中:

  • 不要使用Write-Error,因为类的设计目的不是发出非终止错误。

    • 您看到任何输出的唯一原因是 PowerShell Core 7.0.0-rc.3 中的一个错误,其方法的返回类型恰好是 [void] - 参见 GitHub issue #5331 .
  • 相反,仅通过使用 Throw 语句抛出错误或通过捕获终止 错误(包括来自 .NET 方法的异常和带有 -ErrorAction Stop 的 cmdlet 调用)。

    • 注意:Throw-ErrorAction Stop(或 $ErrorActionPreference = 'Stop')创建脚本-终止(线程终止)错误,而 .NET 方法抛出的异常(未在类方法中捕获并重新抛出)仅创建语句 - 终止错误;也就是说,虽然类方法主体立即终止,但默认情况下在调用者中继续执行;后者还适用于未找到可执行文件的调用运算符 (&)、1/0 等表达式中的错误以及发出语句终止错误的 cmdlet 调用(他们可以报告的最严重的错误类型)没有他们被提升为脚本-使用-ErrorAction Stop终止错误类型;见this GitHub docs issue全面了解 PowerShell 的复杂错误处理。

参见 this answer以获取更多关于类方法中的错误处理和流输出行为的信息。

这是您的代码的更正版本。

class Operator {
    [void] oper1() {
        Try {
            # Try to invoke a non-existent executable.
            & ".\some_exe_which_does_not_exist.exe"
        }
        Catch {
            # Re-throw the error.
            # Alternatively, don't use try / catch, but the error
            # then only aborts the method call, not the entire script.
            Throw
        }
    }
}

$operator = [Operator]::new()
$operator.oper1()

关于powershell - Write-Error 在类方法内部使用时不会产生有用的信息 |电源外壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60482170/

相关文章:

powershell - 替换文件中的字符串,并获得有关所做操作的一些反馈

powershell - PowerShell 模块中的变量

powershell - 如果帐户被锁定,请发送消息

java - 同一类中的类似方法

class - MVVM View 模型命名

c++ - 在 C++ 中定义一个类

python - Python-小小的初学者代码困惑

unit-testing - 为什么VS2015中的TestExplorer加载Pester模块失败?

android - Flutter android 在 Release模式下灰屏,即使在 Debug模式下没有错误或红屏

php - 从同一类中的静态方法访问另一个方法中的变量