![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||