考虑以下脚本
\n\n#requires -version 2.0\n\n[CmdletBinding()]\nparam\n(\n)\n\n$script:ErrorActionPreference = "Stop"\nSet-StrictMode -Version Latest\nfunction PSScriptRoot { $MyInvocation.ScriptName | Split-Path }\n\nfunction ThrowFunction($i)\n{\n "ThrowFunction $i"\n $someNonExistingVariable\n}\n\n@(1, 2, 3) | ForEach-Object -Process { ThrowFunction $_ }\nRun Code Online (Sandbox Code Playgroud)\n\n当我们运行它时我们得到
\n\nC:\\dev> .\\MyScript.ps1\nThrowFunction 1\nForEach-Object : The variable \'$someNonExistingVariable\' cannot be retrieved because it has not been set.\nAt C:\\dev\\MyScript.ps1:18 char:28\n+ @(1, 2, 3) | ForEach-Object <<<< -Process { ThrowFunction $_ }\n + CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [ForEach-Object], RuntimeException\n + FullyQualifiedErrorId : VariableIsUndefined,Microsoft.PowerShell.Commands.ForEachObjectCommand\nRun Code Online (Sandbox Code Playgroud)\n\n正如您所看到的,它在第 18 行报告了问题
\n\n但实际的问题是在第 #15 行
\n\n我发现如果我们改变第8行:
\n\n$script:ErrorActionPreference = "Continue"\nRun Code Online (Sandbox Code Playgroud)\n\n我们得到
\n\nC:\\dev> .\\MyScript.ps1\nThrowFunction 1\nThe variable \'$someNonExistingVariable\' cannot be retrieved because it has not been set.\nAt C:\\dev\\MyScript.ps1:15 char:29\n+ $someNonExistingVariable <<<<\n + CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException\n + FullyQualifiedErrorId : VariableIsUndefined\n\nThrowFunction 2\nThe variable \'$someNonExistingVariable\' cannot be retrieved because it has not been set.\nAt C:\\dev\\MyScript.ps1:15 char:29\n+ $someNonExistingVariable <<<<\n + CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException\n + FullyQualifiedErrorId : VariableIsUndefined\n\nThrowFunction 3\nThe variable \'$someNonExistingVariable\' cannot be retrieved because it has not been set.\nAt C:\\dev\\MyScript.ps1:15 char:29\n+ $someNonExistingVariable <<<<\n + CategoryInfo : InvalidOperation: (someNonExistingVariable:Token) [], RuntimeException\n + FullyQualifiedErrorId : VariableIsUndefined\nRun Code Online (Sandbox Code Playgroud)\n\n您会看到现在第 15 行已按预期报告。
\n\n现在的问题是如何获得正确的行并具有 \xe2\x80\x9cStop\xe2\x80\x9d 行为。
\n\n我尝试了很多方法,但没有一个对我有用。
\n\n我试过
\n\ntrap { throw $_ }\n\ntrap { $_.InvocationInfo }\n\ntrap { Get-PSCallStack }\nRun Code Online (Sandbox Code Playgroud)\n\n但他们都没有找到正确的路线
\n\n然后我尝试切换
\n\n$script:ErrorActionPreference = "Continue"\nRun Code Online (Sandbox Code Playgroud)\n\n发现只要添加任何trap,就又报错行了。
\n\n所以我仍在寻找可行的解决方案......
\n感谢@Keith Hill,我找到了解决方案
魔法线是
trap { throw $Error[0] }
Run Code Online (Sandbox Code Playgroud)
这个脚本
#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 $_ }
Run Code Online (Sandbox Code Playgroud)
回报
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
Run Code Online (Sandbox Code Playgroud)
伟大的!
| 归档时间: |
|
| 查看次数: |
1122 次 |
| 最近记录: |