gpt4 book ai didi

powershell - 如何使用 $ErrorActionPreference = "Stop"获得正确的错误行号

转载 作者:行者123 更新时间:2023-12-03 14:15:40 24 4
gpt4 key购买 nike

考虑以下脚本

#requires -version 2.0

[CmdletBinding()]
param
(
)

$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function PSScriptRoot { $MyInvocation.ScriptName | Split-Path }

function ThrowFunction($i)
{
"ThrowFunction $i"
$someNonExistingVariable
}

@(1, 2, 3) | ForEach-Object -Process { ThrowFunction $_ }

当我们运行它时,我们得到

C:\dev> .\MyScript.ps1
ThrowFunction 1
ForEach-Object : The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At C:\dev\MyScript.ps1:18 char:28
+ @(1, 2, 3) | ForEach-Object <<<< -Process { ThrowFunction $_ }
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [ForEach-Object], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand

如您所见,它在第 18 行报告了问题

但实际问题在第 15 行

我发现如果我们更改第 8 行:

$script:ErrorActionPreference = "Continue"

我们得到

C:\dev> .\MyScript.ps1
ThrowFunction 1
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At C:\dev\MyScript.ps1:15 char:29
+ $someNonExistingVariable <<<<
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

ThrowFunction 2
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At C:\dev\MyScript.ps1:15 char:29
+ $someNonExistingVariable <<<<
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

ThrowFunction 3
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At C:\dev\MyScript.ps1:15 char:29
+ $someNonExistingVariable <<<<
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

您会看到,现在第 15 行已按预期报告。

现在的问题是如何获得正确的线路并具有“停止”行为。

我尝试了很多方法,但没有一个对我有用。

我试过了

trap { throw $_ }

trap { $_.InvocationInfo }

trap { Get-PSCallStack }

但他们都没有得到正确的线路

然后我尝试切换

$script:ErrorActionPreference = "Continue"

发现我一加trap就又报错行了。

所以我仍在寻找可行的解决方案...

最佳答案

感谢@Keith Hill,我找到了解决方案

魔法线是

trap { throw $Error[0] }

这个脚本

#requires -version 2.0

[CmdletBinding()]
param
(
)

$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function PSScriptRoot { $MyInvocation.ScriptName | Split-Path }

trap { throw $Error[0] }

function ThrowFunction($i)
{
"ThrowFunction $i"
$someNonExistingVariable
}

@(1, 2, 3) | ForEach-Object -Process { ThrowFunction $_ }

返回

C:\Dev> .\MyScript.ps1
ThrowFunction 1
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At C:\Dev\MyScript.ps1:17 char:29
+ $someNonExistingVariable <<<<
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

太棒了!

关于powershell - 如何使用 $ErrorActionPreference = "Stop"获得正确的错误行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com