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 - Write-Error doesn't write anyting

Reply
 
Old 05-08-2009   #1 (permalink)
stej


 
 

Write-Error doesn't write anyting

I created a new object using new-object PSObject and added a
ScriptMethod. In some cases the method should write error, but it
doesn't.

Consider this code:
$ErrorActionPreference='stop'
New-Object PSObject | Add-Member scriptmethod w {write-error "NO"} -
pass | % { $_.w() };
^__ this works, error is written and the execution is stopped.

But the following code doesn't write anything even when it should:
$ErrorActionPreference='continue' #this is the only change
New-Object PSObject | Add-Member scriptmethod w {write-error "NO"} -
pass | % { $_.w() };

The error is added to $error, but not written to console. Note that
this works:
$ErrorActionPreference='continue' #this is the only change
write-error "error"; get-date

Why write-error doesn't work inside ScriptMethod?

My System SpecsSystem Spec
Old 05-11-2009   #2 (permalink)
Bob Landau


 
 

RE: Write-Error doesn't write anyting

What version of Powershell if v2 what does $psversiontable show?

The below is what I'm seeing with CTP3:

1) Setting the Powershell variable $ErrorActionPerference to 'stop' does not
stop Write-Error from being sent to the console.

2) Unfortunately in your case what I'm seeing is looks to be a bug in
Powershell. Here is what I mean (ErrorActionPerference is in the default
state)

$o = New-Object PSObject | Add-Member -MemberType ScriptMethod {write-error
hello} -Name w -pass

$o.w ## No surprise here, this dump the properties of this member which is
a scriptmethod

$o.w() ## nothing is written to the console

Create a new PSObject ($o1) but use Write-Host/Warning/Output to outuput
the message

$o1.w() ## this will write to the console

Now set ErrorActionPreference to "stop" then when you call $o.w() the error
I see is not "hello" but

"can't call w with 0 arguments ...."

are you seeing the same?

While setting the preference did write an error to the console its not what
I expected. The expected results should be "Hello".

This is the reason I think you should submit a bug report.

bob

"stej" wrote:
Quote:

> I created a new object using new-object PSObject and added a
> ScriptMethod. In some cases the method should write error, but it
> doesn't.
>
> Consider this code:
> $ErrorActionPreference='stop'
> New-Object PSObject | Add-Member scriptmethod w {write-error "NO"} -
> pass | % { $_.w() };
> ^__ this works, error is written and the execution is stopped.
>
> But the following code doesn't write anything even when it should:
> $ErrorActionPreference='continue' #this is the only change
> New-Object PSObject | Add-Member scriptmethod w {write-error "NO"} -
> pass | % { $_.w() };
>
> The error is added to $error, but not written to console. Note that
> this works:
> $ErrorActionPreference='continue' #this is the only change
> write-error "error"; get-date
>
> Why write-error doesn't work inside ScriptMethod?
>
My System SpecsSystem Spec
Old 05-11-2009   #3 (permalink)
stej


 
 

Re: Write-Error doesn't write anyting

On May 11, 11:31*pm, Bob Landau <BobLan...@xxxxxx>
wrote:
Quote:

> What version of Powershell if v2 what does $psversiontable show?
> The below is what I'm seeing with CTP3:
I use CTP3 as well
Quote:

> 1) Setting the Powershell variable $ErrorActionPerference to 'stop' does not
> stop Write-Error from being sent to the console.
When v$ErrorActionPerference is 'stop', write-error should write to
console, it's ok. But the execution of the script/function/.. should
stop.
Quote:

> 2) Unfortunately in your case what I'm seeing is looks to be a bug in
> Powershell. Here is what I mean (ErrorActionPerference is in the default
> state)
>
> $o = New-Object PSObject | Add-Member -MemberType ScriptMethod {write-error
> hello} -Name w -pass
> $o.w *## No surprise here, *this dump the properties of this member which is
> a scriptmethod
>
> $o.w() *## nothing is written to the console
>
> Create a new PSObject *($o1) but use Write-Host/Warning/Output to outuput
> the message
>
> $o1.w() ## this will write to the console
>
> Now set ErrorActionPreference to "stop" then when you call $o.w() the error
> I see is not "hello" but
>
> "can't call w with 0 arguments ...."
>
> are you seeing the same?
>
> While setting the preference did write an error to the console its not what
> I expected. The expected results should be "Hello".
>
> This is the reason I think you should submit a bug report.
I tried this code:

$o = New-Object PSObject | Add-Member -MemberType ScriptMethod {write-
error hello } -name w -pass
$ErrorActionPreference = 'continue'
$o.w() # prints nothing
$ErrorActionPreference = "stop"
$o.w() #stops and writes error
Quote:
Quote:

>> Exception calling "w" with "0" argument(s): "Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: hello"
It should work for you in the same way.
My System SpecsSystem Spec
Old 05-11-2009   #4 (permalink)
Bob Landau


 
 

Re: Write-Error doesn't write anyting

Stej,

Yes, we are both seeing the same thing; as I said in the previous post I
believe you found a bug. Do you know how to report it? Do you want to report
it?

For now a work-around is to use Write-Warning which for me will work using
v2 CTP3. At least the users of your script will be able to differentiate
normal output from output to be concerned about.


"stej" wrote:
Quote:

> On May 11, 11:31 pm, Bob Landau <BobLan...@xxxxxx>
> wrote:
Quote:

> > What version of Powershell if v2 what does $psversiontable show?
> > The below is what I'm seeing with CTP3:
> I use CTP3 as well
>
Quote:

> > 1) Setting the Powershell variable $ErrorActionPerference to 'stop' does not
> > stop Write-Error from being sent to the console.
> When v$ErrorActionPerference is 'stop', write-error should write to
> console, it's ok. But the execution of the script/function/.. should
> stop.
>
Quote:

> > 2) Unfortunately in your case what I'm seeing is looks to be a bug in
> > Powershell. Here is what I mean (ErrorActionPerference is in the default
> > state)
> >
> > $o = New-Object PSObject | Add-Member -MemberType ScriptMethod {write-error
> > hello} -Name w -pass
> > $o.w ## No surprise here, this dump the properties of this member which is
> > a scriptmethod
> >
> > $o.w() ## nothing is written to the console
> >
> > Create a new PSObject ($o1) but use Write-Host/Warning/Output to outuput
> > the message
> >
> > $o1.w() ## this will write to the console
> >
> > Now set ErrorActionPreference to "stop" then when you call $o.w() the error
> > I see is not "hello" but
> >
> > "can't call w with 0 arguments ...."
> >
> > are you seeing the same?
> >
> > While setting the preference did write an error to the console its not what
> > I expected. The expected results should be "Hello".
> >
> > This is the reason I think you should submit a bug report.
>
> I tried this code:
>
> $o = New-Object PSObject | Add-Member -MemberType ScriptMethod {write-
> error hello } -name w -pass
> $ErrorActionPreference = 'continue'
> $o.w() # prints nothing
> $ErrorActionPreference = "stop"
> $o.w() #stops and writes error
Quote:
Quote:

> >> Exception calling "w" with "0" argument(s): "Command execution stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: hello"
>
> It should work for you in the same way.
>
My System SpecsSystem Spec
Old 05-27-2009   #5 (permalink)
stej


 
 

Re: Write-Error doesn't write anyting

I asked at twitter how to report a bug, nobody answered. Do you know
where to go?
My System SpecsSystem Spec
Old 05-27-2009   #6 (permalink)
tojo2000


 
 

Re: Write-Error doesn't write anyting

On May 26, 11:02*pm, stej <cerna.ze...@xxxxxx> wrote:
Quote:

> I asked at twitter how to report a bug, nobody answered. Do you know
> where to go?
Set up an account at Microsoft Connect http://connect.microsoft.com/,
and you should be able to submit feedback.
My System SpecsSystem Spec
Old 05-27-2009   #7 (permalink)
stej


 
 

Re: Write-Error doesn't write anyting

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Cannot write to drive "Media is write protected" Vista hardware & devices
how to get powershell to write to event log or write a log file? PowerShell
Write error System Security
The disk is write-protected. Remove the write-protection or... Vista General
newline in Write-Debug or Write-Verbose 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