Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

I'm feeling Trap'ped

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-03-2007   #1 (permalink)
Knox
Guest


 

I'm feeling Trap'ped

I have a function that I'm attempting to use Trap in. I'm a total newbie to
trapping. After my function does some routine kind of stuff that doesn't
cause errors, I have the following:

Trap {
add-member -InputObject $nr -MemberType NoteProperty -name
"status" -value "UN1"
write-host "REAL PROBLEM caught"
return $nr
}

$tmf = (get-childitem -path $np | sort-object | select-object -last
1)

Powershell writes to the error stream the following error

Get-ChildItem : An object at the specified path \\N36\blah does not
exist.
At C:\documents and settings\Knox\my documents\knoxPower\nx2.ps1:29
char:28
+ $tmf = (get-childitem <<<< -path $np | sort-object |
select-object -last 1)

Clearly that's exactly what I'm trying to detect with trap and prevent. I
never see the "REAL PROBLEM" printed.
BTW, I know I could do a test-path and prevent an error. I'm trying to
understand Trap.

I appreciate your input.

Knox


My System SpecsSystem Spec
Old 08-03-2007   #2 (permalink)
RichS
Guest


 

RE: I'm feeling Trap'ped

I tried

trap {"Exception " + $_; continue}

get-childitem -path "c:\scripts\testz.txt"

knowing that the file did not exist and got this

Get-ChildItem : Cannot find path 'C:\scripts\testz.txt' because it does not
exist.
At line:1 char:14
+ Get-ChildItem <<<< -Path "C:\scripts\testz.txt"

The trap should have picked up any exception that was thrown. Which makes
me think that the cmdlet may not be throwing an exception in this case
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Knox" wrote:

> I have a function that I'm attempting to use Trap in. I'm a total newbie to
> trapping. After my function does some routine kind of stuff that doesn't
> cause errors, I have the following:
>
> Trap {
> add-member -InputObject $nr -MemberType NoteProperty -name
> "status" -value "UN1"
> write-host "REAL PROBLEM caught"
> return $nr
> }
>
> $tmf = (get-childitem -path $np | sort-object | select-object -last
> 1)
>
> Powershell writes to the error stream the following error
>
> Get-ChildItem : An object at the specified path \\N36\blah does not
> exist.
> At C:\documents and settings\Knox\my documents\knoxPower\nx2.ps1:29
> char:28
> + $tmf = (get-childitem <<<< -path $np | sort-object |
> select-object -last 1)
>
> Clearly that's exactly what I'm trying to detect with trap and prevent. I
> never see the "REAL PROBLEM" printed.
> BTW, I know I could do a test-path and prevent an error. I'm trying to
> understand Trap.
>
> I appreciate your input.
>
> Knox
>
>

My System SpecsSystem Spec
Old 08-03-2007   #3 (permalink)
Knox
Guest


 

Re: I'm feeling Trap'ped

Hi, Richard

Thank you for testing it. I guess I should use Test-Path, but later in the
function I have some WMI stuff that should also be trapped and isn't. I was
hoping that the trap would work.

Thanks,

(I enjoy your blog BTW.)

Knox



"RichS" <RichS@discussions.microsoft.com> wrote in message
news:ED27DD0B-07C5-46EA-B53B-53E23E54348D@microsoft.com...
>I tried
>
> trap {"Exception " + $_; continue}
>
> get-childitem -path "c:\scripts\testz.txt"
>
> knowing that the file did not exist and got this
>
> Get-ChildItem : Cannot find path 'C:\scripts\testz.txt' because it does
> not
> exist.
> At line:1 char:14
> + Get-ChildItem <<<< -Path "C:\scripts\testz.txt"
>
> The trap should have picked up any exception that was thrown. Which makes
> me think that the cmdlet may not be throwing an exception in this case
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Knox" wrote:
>
>> I have a function that I'm attempting to use Trap in. I'm a total newbie
>> to
>> trapping. After my function does some routine kind of stuff that doesn't
>> cause errors, I have the following:
>>
>> Trap {
>> add-member -InputObject $nr -MemberType NoteProperty -name
>> "status" -value "UN1"
>> write-host "REAL PROBLEM caught"
>> return $nr
>> }
>>
>> $tmf = (get-childitem -path $np | sort-object |
>> select-object -last
>> 1)
>>
>> Powershell writes to the error stream the following error
>>
>> Get-ChildItem : An object at the specified path \\N36\blah does
>> not
>> exist.
>> At C:\documents and settings\Knox\my
>> documents\knoxPower\nx2.ps1:29
>> char:28
>> + $tmf = (get-childitem <<<< -path $np | sort-object |
>> select-object -last 1)
>>
>> Clearly that's exactly what I'm trying to detect with trap and prevent.
>> I
>> never see the "REAL PROBLEM" printed.
>> BTW, I know I could do a test-path and prevent an error. I'm trying to
>> understand Trap.
>>
>> I appreciate your input.
>>
>> Knox
>>
>>


My System SpecsSystem Spec
Old 08-03-2007   #4 (permalink)
Knox
Guest


 

Re: I'm feeling Trap'ped

Richard,
One other interesting thing. I get the red text as if the error is normal.
However I am unable to pipe the error text using the 2> operation. if you
haven't thrown away your test bed, I'm curious if you can pipe the error
coming out of Get-ChildItem.

Thanks,

Knox

"RichS" <RichS@discussions.microsoft.com> wrote in message
news:ED27DD0B-07C5-46EA-B53B-53E23E54348D@microsoft.com...
>I tried
>
> trap {"Exception " + $_; continue}
>
> get-childitem -path "c:\scripts\testz.txt"
>
> knowing that the file did not exist and got this
>
> Get-ChildItem : Cannot find path 'C:\scripts\testz.txt' because it does
> not
> exist.
> At line:1 char:14
> + Get-ChildItem <<<< -Path "C:\scripts\testz.txt"
>
> The trap should have picked up any exception that was thrown. Which makes
> me think that the cmdlet may not be throwing an exception in this case
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Knox" wrote:
>
>> I have a function that I'm attempting to use Trap in. I'm a total newbie
>> to
>> trapping. After my function does some routine kind of stuff that doesn't
>> cause errors, I have the following:
>>
>> Trap {
>> add-member -InputObject $nr -MemberType NoteProperty -name
>> "status" -value "UN1"
>> write-host "REAL PROBLEM caught"
>> return $nr
>> }
>>
>> $tmf = (get-childitem -path $np | sort-object |
>> select-object -last
>> 1)
>>
>> Powershell writes to the error stream the following error
>>
>> Get-ChildItem : An object at the specified path \\N36\blah does
>> not
>> exist.
>> At C:\documents and settings\Knox\my
>> documents\knoxPower\nx2.ps1:29
>> char:28
>> + $tmf = (get-childitem <<<< -path $np | sort-object |
>> select-object -last 1)
>>
>> Clearly that's exactly what I'm trying to detect with trap and prevent.
>> I
>> never see the "REAL PROBLEM" printed.
>> BTW, I know I could do a test-path and prevent an error. I'm trying to
>> understand Trap.
>>
>> I appreciate your input.
>>
>> Knox
>>
>>


My System SpecsSystem Spec
Old 08-05-2007   #5 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: I'm feeling Trap'ped

Richard is correct about the cmdlet not throwing an error if the file does
not exist. Instead, it writes to the error output stream. If you want to
force it to throw an error, then set the -erroraction property to "stop" as
shown in the following example.

We'll start with Richard's original example, trimmed to fix on one line.

PS (1) > trap {"Exception $_"; continue} ; gci c:\nosuchpath
Get-ChildItem : Cannot find path 'C:\nosuchpath' because it does not exist.
At line:1 char:38
+ trap {"Exception $_"; continue} ; gci <<<< c:\nosuchpath

This generates the normal error display because the cmdlet is writing to the
error stream instead of throwing an exception. The trap statement is not
executed. Now let's use -ErrorAction (-ea) to change the behaviour of the
cmdlet so instead of writing a series of error objects to the error stream,
it throws an exception on the first error:

PS (2) > trap {"Exception $_"; continue} ; gci -ea stop c:\nosuchpath
Exception Command execution stopped because the shell variable
"ErrorActionPref
erence" is set to Stop: Cannot find path 'C:\nosuchpath' because it does not
ex
ist.

This time the exception is thrown, is caught by the trap handler and
displayed on the console.

You can change the default error action in a scope by setting the preference
variable $ErrorActionPreference to "stop".

PS (3) > $ErrorActionPreference="stop" # setting at the global level
affects all subsequent commands...

Now an exception will always be thrown even though -ea was not specified.

PS (4) > trap {"Exception $_"; continue} ; gci -ea stop c:\nosuchpath
Exception Command execution stopped because the shell variable
"ErrorActionPref
erence" is set to Stop: Cannot find path 'C:\nosuchpath' because it does not
ex
ist.

Hopefully this clears things up a bit. (BTW - I cover this area in detail in
section 9.1.4 of my book...)

I'm not sure about the problem with redirecting the error output though. It
works fine in the following example:

PS (5) > gci c:\nosuchpath 2> $null
PS (6) > $?
False
PS (7) >

As you can see, no message was displayed - redirecting to $null simply
discarded the message. You can still tell that an error occurred because the
value of $? is set to $false.

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell

"Knox" <thorn99KILLCAPS@hotKILLmail.com> wrote in message
news:446F1CD1-8CDA-4C08-B113-D343D51E3EEA@microsoft.com...
> Richard,
> One other interesting thing. I get the red text as if the error is
> normal. However I am unable to pipe the error text using the 2> operation.
> if you haven't thrown away your test bed, I'm curious if you can pipe the
> error coming out of Get-ChildItem.
>
> Thanks,
>
> Knox
>
> "RichS" <RichS@discussions.microsoft.com> wrote in message
> news:ED27DD0B-07C5-46EA-B53B-53E23E54348D@microsoft.com...
>>I tried
>>
>> trap {"Exception " + $_; continue}
>>
>> get-childitem -path "c:\scripts\testz.txt"
>>
>> knowing that the file did not exist and got this
>>
>> Get-ChildItem : Cannot find path 'C:\scripts\testz.txt' because it does
>> not
>> exist.
>> At line:1 char:14
>> + Get-ChildItem <<<< -Path "C:\scripts\testz.txt"
>>
>> The trap should have picked up any exception that was thrown. Which
>> makes
>> me think that the cmdlet may not be throwing an exception in this case
>> --
>> Richard Siddaway
>> Please note that all scripts are supplied "as is" and with no warranty
>> Blog: http://richardsiddaway.spaces.live.com/
>> PowerShell User Group: http://www.get-psuguk.org.uk
>>
>>
>> "Knox" wrote:
>>
>>> I have a function that I'm attempting to use Trap in. I'm a total
>>> newbie to
>>> trapping. After my function does some routine kind of stuff that doesn't
>>> cause errors, I have the following:
>>>
>>> Trap {
>>> add-member -InputObject $nr -MemberType NoteProperty -name
>>> "status" -value "UN1"
>>> write-host "REAL PROBLEM caught"
>>> return $nr
>>> }
>>>
>>> $tmf = (get-childitem -path $np | sort-object |
>>> select-object -last
>>> 1)
>>>
>>> Powershell writes to the error stream the following error
>>>
>>> Get-ChildItem : An object at the specified path \\N36\blah does
>>> not
>>> exist.
>>> At C:\documents and settings\Knox\my
>>> documents\knoxPower\nx2.ps1:29
>>> char:28
>>> + $tmf = (get-childitem <<<< -path $np | sort-object |
>>> select-object -last 1)
>>>
>>> Clearly that's exactly what I'm trying to detect with trap and prevent.
>>> I
>>> never see the "REAL PROBLEM" printed.
>>> BTW, I know I could do a test-path and prevent an error. I'm trying to
>>> understand Trap.
>>>
>>> I appreciate your input.
>>>
>>> Knox
>>>
>>>

>



My System SpecsSystem Spec
Old 08-06-2007   #6 (permalink)
Knox
Guest


 

Re: I'm feeling Trap'ped

Hi, Bruce

Thank you very much for clearing up my misunderstanding. Although I read
your book from cover to cover, when I came back later to implement the trap
instruction, I focused on chapter 9.2 and missed the critical detail at the
beginning of 9.2 that trap only traps terminating errors. Very cool how it
is implemented.

Your book is excellent and I really enjoyed that it's not just a primer, but
explains why your group did things the way you did. And in some cases, how
you tried alternatives.

I will investigate my remaining issue of the redirection of the error text.

Thanks again!

Knox


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista with feeling NjetSpam Vista General 2 12-29-2007 03:39 PM
DreamScene Leaving Me Feeling Ripped OFF! Dave Reid Vista General 17 02-02-2007 03:31 AM
Vista has a linux feeling to it MicroFox Vista General 14 11-20-2006 05:36 PM
I'm Feeling Lucky Today! kevpan815 Vista General 8 11-16-2006 11:42 PM
Paul Thurrot feeling the heat? MICHAEL Vista General 38 10-30-2006 07:55 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008