Skip to content
mnaoumov.dev
Go back

Write-Error vs error tracing

Hi folks

The only unresolved problem with error tracing left is the problem with Write-Error

Consider the script

$someComplexCondition = $false

if ($someComplexCondition)
{
    Write-Error -Message "Some complex condition"
}
else
{
    Write-Error -Message "Other complex condition"
}

When I run it it says

C:\Dev> .\MyScript.ps1
C:\Dev\MyScript.ps1 : Other complex condition
At line:1 char:15
+ .\MyScript.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,MyScript.ps1

And I noticed that Write-Error always reports itself as at line:1 According to the stack trace it looks like it’s line #1 because it was called from the shell and more interesting char:15 because

".\MyScript.ps1 ".Length -eq 15

If we change file name, this char:15 will be changed accordingly.

The question is how to get the actual line when the error occurred.

In our case I would like to get line:9

I did not find a solution.

Raised a question on StackOverflow: http://stackoverflow.com/questions/27654028/get-a-line-number-where-write-error-occurred/27656478


Share this post on:

Previous Post
sqlcmd - Get real sql error line number
Next Post
PowerShell $ErrorActionPreference = "Stop" vs error tracing