How to get the correct error line number using $ ErrorActionPreference = "Stop"
Consider the following script
#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 $_ }
When we run it we get
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
As you can see it reports the problem on line # 18
But the actual problem is line # 15
I found that if we change line 8:
$script:ErrorActionPreference = "Continue"
We get
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
And you can see that now line 15 is reported as expected.
Now the question is how to get the correct string and have the "Stop" behavior.
I tried many approaches and none of them worked for me.
I tried
trap { throw $_ }
trap { $_.InvocationInfo }
trap { Get-PSCallStack }
but none of them get the correct string
Then I tried to switch
$script:ErrorActionPreference = "Continue"
and found that as soon as I add any trap, the wrong line is reported again.
So, I'm still looking for a job for a solution ...
source to share
Thanks to @Keith Hill I found a solution
Magic line
trap { throw $Error[0] }
This script
#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 $_ }
returns
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
Fine!
source to share
Even if you set strictmode to Latest, it $someNonExistingVariable
will not generate a trailing error. It just writes to the error stream. By setting the ErrorActionPreference to Stop
, you now have a Foreach-Object that converts an endless error to a trailing error. This is why the error indicates line 18 - the Foreach-Object cmdlet.
If you look in the $ Error collection assuming Clear () first, you will see two errors. The latter is at $ Error [0], which is a trailing error thrown by Foreach-Object. $ Error [1] is a non-termination error generated by an attempt to access the undefined variable. It has the correct line number - 15.
You can access the ScriptStackTrace like this:
PS C:\> $error[0].errorRecord.ScriptStackTrace
at ThrowFunction, C:\Users\hillr\ErrorLine.ps1: line 15
at <ScriptBlock>, C:\Users\hillr\ErrorLine.ps1: line 18
at <ScriptBlock>, C:\Users\hillr\ErrorLine.ps1: line 18
source to share
You can try {} Catch {} instead:
#requires -version 2.0
[CmdletBinding()]
param
(
)
$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function PSScriptRoot { $MyInvocation.ScriptName | Split-Path }
function ThrowFunction($i)
{
"ThrowFunction $i"
Try {
$someNonExistingVariable
}
Catch { # The variable $_ represents the error that is caught
Write-Output $_
}
}
@(1, 2, 3) | ForEach-Object -Process { ThrowFunction $_ }
And the resulting output gives the correct line number:
ThrowFunction 1
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At line:16 char:9
+ $someNonExistingVariable
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
ThrowFunction 2
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At line:16 char:9
+ $someNonExistingVariable
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
ThrowFunction 3
The variable '$someNonExistingVariable' cannot be retrieved because it has not been set.
At line:16 char:9
+ $someNonExistingVariable
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (someNonExistingVariable:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
source to share