ErrorAction "SilentlyContinue" 不填充 ErrorVariable

ErrorAction "SilentlyContinue" does not populate ErrorVariable

问题

使用 -ErrorAction "SilentlyContinue" 时,未按照以下文档中的描述填充错误变量:Handling Errors the PowerShell Way

问题

如何防止错误像 -ErrorAction "Continue" 一样显示,但仍然填充错误变量?

奖金问题

有没有办法将错误附加到错误变量以存储多个错误?

MWE

脚本

$ErrorActions = @("Continue", "SilentlyContinue", "Stop")
foreach ($ErrorAction in $ErrorActions) {
    Write-Host -Object $ErrorAction -ForegroundColor "Green"
    Get-Item -Path "C:\tmp\error1" -ErrorAction $ErrorAction -ErrorVariable "ErrorMessage"
    Get-Item -Path "C:\tmp\error2" -ErrorAction $ErrorAction -ErrorVariable "ErrorMessage"
    Write-Host -Object "ErrorVariable" -ForegroundColor "Yellow"
    $ErrorMessage
}

输出

Continue

Get-Item : Cannot find path 'C:\tmp\error1' because it does not exist.

At C:\tmp\mwe.ps1:43 char:5

  • Get-Item -Path "C:\tmp\error1" -ErrorAction $ErrorAction -ErrorVa ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (C:\tmp\error1:String) [Get-Item], ItemNotFoundException

    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Get-Item : Cannot find path 'C:\tmp\error2' because it does not exist.

At C:\tmp\mwe.ps1:44 char:5

  • Get-Item -Path "C:\tmp\error2" -ErrorAction $ErrorAction -ErrorVa ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (C:\tmp\error2:String) [Get-Item], ItemNotFoundException

    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

ErrorVariable

Get-Item : Cannot find path 'C:\tmp\error2' because it does not exist.

At C:\tmp\mwe.ps1:44 char:5

  • Get-Item -Path "C:\tmp\error2" -ErrorAction $ErrorAction -ErrorVa ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (C:\tmp\error2:String) [Get-Item], ItemNotFoundException

    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

SilentlyContinue

ErrorVariable

Stop

Get-Item : Cannot find path 'C:\tmp\error1' because it does not exist.

At C:\tmp\mwe.ps1:43 char:5

  • Get-Item -Path "C:\tmp\error1" -ErrorAction $ErrorAction -ErrorVa ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (C:\tmp\error1:String) [Get-Item], ItemNotFoundException

    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Get-ChildItem为例:

Get-ChildItem C:\nonexist -EA SilentlyContinue -EV silentErr
[bool]$silentErr # ==> True

正如您链接到的文档中提到的那样,-ErrorVariable$ErrorActionPreference 在发生非终止错误时控制以下行为(这些 不是 影响 终止错误):

  • Continue:显示错误并继续执行。
  • SilentlyContinue:隐藏错误但仍将其添加到 -ErrorVariable,如果指定,和 $error 堆栈。
  • Stop:抛出异常并停止执行。可以通过 try / catch / finally 块捕获和处理异常。
    • 非终止 错误无法通过 try / catch / finally 通常处理,因为不会抛出异常,除非 -ErrorActionStop.
  • Ignore:错误被吞没,没有迹象表明它发生了。在这种情况下,-ErrorVariable$error 堆栈都不会更新。
  • Inquire: 询问接线员如果出现错误怎么办

利用上面的知识,如果-ErrorAction是[=16,我们仍然可以依靠$error自动变量和指定的
-ErrorVariable来填充=].

Note: You can also use the $ErrorView variable to further control how errors are displayed in the console.


要回答您的子问题,没有提供“命名”错误堆栈的机制,但您可以使用内置的 $error 堆栈来跟踪发生的错误(和没有出现在
-EA Ignore)。如果你想确保你只在你的 script/session/etc 中的某个点之前发生错误,你可以调用以下命令来清除错误变量:

$error.Clear()

附加信息