【问题标题】:Can I make the PowerShell $error object behave consistently in both script and interactive mode?我可以使 PowerShell $error 对象在脚本和交互模式下的行为一致吗?
【发布时间】:2012-07-25 16:32:01
【问题描述】:

我正在编写一个使用$error 来检测和响应错误的PowerShell 脚本。我的问题是 $error 对象的行为不同,具体取决于我运行脚本的方式。如果我以交互方式运行它(特别是从 PowerShell ISE),则会将错误添加到集合中,但如果我从命令行运行相同的脚本,则会发生相同的错误,但不会添加到集合中。

这是脚本(为了说明问题而进行了精简):

# export_exception_test.ps1

# show me how many errors before we start
"Count = " + $error.Count

try {
    # treat non-terminating errors as terminating errors.
    $ErrorActionPreference = "Stop"
    # echo the setting to output
    $ErrorActionPreference

    # use sqlcmd to save the output of stored procedures to text files.
    # The first and third call will fail because the output folder does not exist.
    # The second call will succeed.
    'first call to sqlcmd'
    sqlcmd -S myserver\myinstance -E -s `"`t`" -Q "EXEC mydatabase.dbo.FI_Codes" -b -o "B:\Exports_new\FI_Codes.txt"
    'second call to sqlcmd'
    sqlcmd -S myserver\myinstance -E -s `"`t`" -Q "EXEC mydatabase.dbo.FI_Codes" -b -o "B:\Exports_newt\FI_Codes.txt"
    'third call to sqlcmd'
    sqlcmd -S myserver\myinstance -E -s `"`t`" -Q "EXEC mydatabase.dbo.FI_Codes" -b -o "B:\Exports_new\FI_Codes.txt"
    # and a whole bunch more of these...

    # The error count should be two more than when we started.
    "Count = " + $error.Count
    # And this should be the most recent error message
    "Message = " + $error[0].Message

}
catch [Exception]
{
    'exception was caught!!!'
    "Count in catch clause = " + $error.Count
    $_.Exception.Message
    # the ultimate goal is to return a non-successful return code when the exports fail
    exit 1
}
finally {
    # set this back to what it was
    $ErrorActionPreference = "Continue"

    # primitive trace output
    'finally.'
}

当我从 PowerShell ISE 运行它时,它的行为符合预期。 SQLCMD 引发一个非终止错误,该错误被捕获和处理。这是输出:

PS U:\> C:\Users\etmatt\Documents\PowerShellScripts\export_exception_test.ps1
Count = 0
Stop
first call to sqlcmd
exception was caught!!!
Count in catch clause = 1
Sqlcmd: Error: Error occurred while opening or operating on file B:\Exports_new\FI_Codes.txt (Reason: The system cannot find the path specified).
finally.

但是当我从命令行运行它时,就像我要设置计划任务一样,$error 不会添加任何内容,也不会引发异常。这是输出:

C:\Users\etmatt>powershell.exe -file "C:\Users\etmatt\Documents\PowerShellScripts\export_exception_test.ps1"
Count = 0
Stop
first call to sqlcmd
Sqlcmd: Error: Error occurred while opening or operating on file B:\Exports_new\FI_Codes.txt (Reason: The system cannot find the path specified).
second call to sqlcmd
third call to sqlcmd
Sqlcmd: Error: Error occurred while opening or operating on file B:\Exports_new\FI_Codes.txt (Reason: The system cannot find the path specified).
Count = 0
Message =
finally.

C:\Users\etmatt>

如果我从 powershell 命令行而不是 cmd.exe(这是有道理的)运行脚本文件,我也会得到相同的结果。例如:

PS C:\> ."C:\Users\etmatt\Documents\PowerShellScripts\export_exception_test.ps1"

我怀疑这与执行上下文有关,或者可能与我仍然不太了解的解析器模式有关,甚至可能与我的个人资料有关(尽管到目前为止,我一直在同一台 PC 下运行所有​​内容同一个账户)。我在网上看到了很多使用try/catch$ErrorActionPreference = "Stop" 的基本方法的示例,所以看来我应该能够完成这项工作。

所以我的问题本质上是:我可以让这个脚本像我认为的那样工作吗?如果不是,我有什么误解?我如何捕捉到这些类型的错误?如果有帮助,我不需要对此进行超级详细的异常处理,我只需要知道什么时候出现问题,以便我的任务监视器可以提醒我。 powershell.exe 的非零返回码就足够了。

我正在研究使用$?$LASTEXITCODE,但我打赌$? 将与$error 有相同的问题,并且两者都只适用于最后执行的语句,这是更少的不是很理想,因为我的真实脚本比这个例子要长很多。

编辑:
好的,从那以后我了解到 Windows 可执行文件(例如 sqlcmd)永远不会向 $error 集合添加任何内容,即使它们返回非零退出代码也是如此。 $error 仅由 cmdlet 使用,如果我理解正确的话。虽然我也可以使用$?,但我通过重复使用$LASTEXITCODE 使我的脚本正常工作,因为它可以识别Windows 可执行退出代码,并且对于非零值设置为false。

总而言之,从命令行运行脚本时的行为是正确的、预期的行为。不过,我仍然不知道为什么我从 PowerShell ISE 得到了不同的结果。

【问题讨论】:

  • 我在运行 SQLCMD 操作时遇到类似的错误,它给我打开或操作文件时出错,但在 CMD shell 中工作正常。我很想知道为什么会发生这种情况

标签: powershell exception-handling powershell-ise


【解决方案1】:

对我来说,我正在使用cmd /C <command> 调用批处理命令,我也遇到了类似的问题。对于非零返回代码 $? 设置正确,但没有 $error 对象(powershell 怎么知道?所以这没关系),代码在 ISE 和命令行执行中的行为相似。

我的问题是,在这种情况下,$ErrorActionPreference = "Stop" 不会引发异常。可能是因为它没有要抛出的$error 对象。所以现在我需要在每次调用其他不整洁的脚本或程序后检查$?。他们应该为这种情况想出一个好的解决方案(可能是通过抛出内置异常说调用外部脚本/程序失败)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2014-09-25
    • 1970-01-01
    • 2011-06-05
    • 2023-04-05
    • 2010-10-26
    相关资源
    最近更新 更多