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

Advertisements

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s