Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Error handling in scripts question

Reply
 
Old 04-30-2007   #1 (permalink)
Keith Hill [MVP]


 
 

Error handling in scripts question

If I create a script (call it foo.ps1) and put the following in it:

function TrapHandler($err) {
"Type of '$err' is $($err.GetType().Fullname)"
"Error: $($Error[0].ErrorRecord)"
"Err: $($err.ErrorRecord)"
"Err: $($_)"
}
trap { TrapHandler $error[0]; continue}

$denom = 0
1/$denom 2>&1 > $null

I get output like so:

Type of 'Attempted to divide by zero.' is System.Management.Automation.ErrorRecord
Error:
Err:
Err: Attempted to divide by zero.

What I want is to also get the line number info which I can't seem to get displayed in a script. However if I execute this from the command prompt after the script completes, I get the additional info that I want:

250# $error[0]
Attempted to divide by zero.
At C:\TFS\CDF\Trunk\Build\test2.ps1:11 char:3
+ 1/$ <<<< denom 2>&1 > $null

What's up with that???

--
Keith

My System SpecsSystem Spec
Old 04-30-2007   #2 (permalink)
Kiron


 
 

RE: Error handling in scripts question

$error's properties have properties also. You can view them with Get-Member.

function TrapHandler($err) {
Write-Host "Error: " -noNewLine; $err.Exception
"Script: $($err.InvocationInfo.ScriptName)"
"Line: $($err.InvocationInfo.ScriptLineNumber)"
"Offset: $($err.InvocationInfo.OffsetInLine)"
}
trap { TrapHandler $error[0]; continue}

$denom = 0
1/$denom 2>&1 > $null

....these are the InvocationInfo properties:
$err.InvocationInfo.MyCommand
$err.InvocationInfo.ScriptLineNumber
$err.InvocationInfo.OffsetInLine
$err.InvocationInfo.ScriptName
$err.InvocationInfo.Line
$err.InvocationInfo.PositionMessage
$err.InvocationInfo.InvocationName
$err.InvocationInfo.PipelineLength
$err.InvocationInfo.PipelinePosition
--
Kiron
My System SpecsSystem Spec
Old 05-01-2007   #3 (permalink)
Keith Hill


 
 

Re: Error handling in scripts question

"Kiron" <Kiron@discussions.microsoft.com> wrote in message
news:F47D067E-8FD5-49D8-98C0-E5E4A112E423@microsoft.com...
> $error's properties have properties also.

Yep I know that. I was able to get what I need from PositionMessage. My
main concern at this point is why does the script behavior differ from the
command line behavior.

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: error handling VB Script
Question with Outlook and scripts Microsoft Office
Quick Question about Error Handling PowerShell
Error Handling PowerShell
Newbie Question: Sample Scripts PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46