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 - Output to STDERR

Reply
 
Old 12-14-2007   #1 (permalink)
Mike Miller


 
 

Output to STDERR

I'm looking for a way to write to STDERR. The only method I've found so far
is write-error, but PowerShell likes to add "useful" information like script
name, arguments, etc. How can this be done?

To elaborate, I'm building a hook for subversion which will deny anyone from
committing too much data at one time. In order to return an error message to
the user, the error needs to be written to STDERR. AFAIK the only way to do
this is with write-error.

So currently, the user gets an error back like this (hyphens mine):
----------------------------------------------------------------------
C:\svn\repos\myrepo\hooks\pre-commit.ps1 :
Your commit exceeds the maximum size allowed by this server.
At line: 1 char: 39
+ c:\svn\repos\myrepo\hooks\pre-commit.ps1 <<<< c:\svn\repos\myrepo 3-1
----------------------------------------------------------------------

Instead of simply:
----------------------------------------------------------------------
Your commit exceeds the maximum size allowed by this server.
----------------------------------------------------------------------

I've tried using write-warning, but this doesn't go to STDERR.

Mike



My System SpecsSystem Spec
Old 12-14-2007   #2 (permalink)
Shay Levi


 
 

Re: Output to STDERR

Hi Mike,

I'm not sure if this is what you need, try:

#$error.clear()
[void]$error.add("Your commit exceeds the maximum size allowed by this server.")
write-host "------------------------" -foregroundColor red -backgroundColor
black
write-host -object $error[0] -fore red -back black
write-host "------------------------" -f red -b black



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I'm looking for a way to write to STDERR. The only method I've found
> so far is write-error, but PowerShell likes to add "useful"
> information like script name, arguments, etc. How can this be done?
>
> To elaborate, I'm building a hook for subversion which will deny
> anyone from committing too much data at one time. In order to return
> an error message to the user, the error needs to be written to STDERR.
> AFAIK the only way to do this is with write-error.
>
> So currently, the user gets an error back like this (hyphens mine):
> ----------------------------------------------------------------------
> C:\svn\repos\myrepo\hooks\pre-commit.ps1 :
> Your commit exceeds the maximum size allowed by this server.
> At line: 1 char: 39
> + c:\svn\repos\myrepo\hooks\pre-commit.ps1 <<<< c:\svn\repos\myrepo
> 3-1
> ----------------------------------------------------------------------
> Instead of simply:
> ----------------------------------------------------------------------
> Your commit exceeds the maximum size allowed by this server.
> ----------------------------------------------------------------------
>
> I've tried using write-warning, but this doesn't go to STDERR.
>
> Mike
>

My System SpecsSystem Spec
Old 12-14-2007   #3 (permalink)
Kiron


 
 

Re: Output to STDERR

# Throw an exception and trap it;
# in the Trap output just the message and exit the script.
# Write-Host and the color variables are optional.

trap {
$errFrgrnd = $host.privatedata.ErrorForegroundColor
$errBckgrnd = $host.privatedata.ErrorBackgroundColor
write-host $_.exception.message -f $errFrgrnd -b $errBckgrnd
exit
}
throw "Your commit exceeds the maximum size allowed by this server."

--
Kiron
My System SpecsSystem Spec
Old 12-17-2007   #4 (permalink)
Mike Miller


 
 

Re: Output to STDERR

Shay, Kiron,

Thanks for giving this a try, but the suggestions you made only simulate the
look of STDERR output in powershell, but is still printed to STDOUT.

If this was a bash script, I would say:
echo "This is written to STDERR" 1>&2

I had noticed this syntax in some of the PowerShell docs. Interestingly
PowerShell will redirect STDERR to STDOUT using this syntax, but throws an
error when trying to use this method to redirect STDOUT to STDERR.

PS>write-error "I'm redirecting STDERR to STDOUT" 2>&1
write-error "I'm redirecting STDERR to STDOUT" 2>&1 : I'm redirecting STDERR
to STDOUT

Or from a script:
PS>& c:\test2.ps1
C:\test2.ps1 : I'm redirecting STDERR to STDOUT
At line:1 char:2
+ & <<<< c:\test2.ps1

But when redirecting STDOUT to STDERR:
PS>write-output "I'm trying to write to STDERR" 1>&2
The redirection operator '1>&2' is not supported yet.
At line:1 char:49
+ write-output "I'm trying to write to STDERR" 1>&2 <<<<

I'd welcome any other ideas. For now I'll use write-stderr. It certainly
isn't unusable for my case, just noisier than necessary.

Mike


"Mike Miller" <mike@xxxxxx> wrote in message
news:OFTqo2pPIHA.484@xxxxxx
Quote:

> I'm looking for a way to write to STDERR. The only method I've found so
> far is write-error, but PowerShell likes to add "useful" information like
> script name, arguments, etc. How can this be done?
>
> To elaborate, I'm building a hook for subversion which will deny anyone
> from committing too much data at one time. In order to return an error
> message to the user, the error needs to be written to STDERR. AFAIK the
> only way to do this is with write-error.
>
> So currently, the user gets an error back like this (hyphens mine):
> ----------------------------------------------------------------------
> C:\svn\repos\myrepo\hooks\pre-commit.ps1 :
> Your commit exceeds the maximum size allowed by this server.
> At line: 1 char: 39
> + c:\svn\repos\myrepo\hooks\pre-commit.ps1 <<<< c:\svn\repos\myrepo 3-1
> ----------------------------------------------------------------------
>
> Instead of simply:
> ----------------------------------------------------------------------
> Your commit exceeds the maximum size allowed by this server.
> ----------------------------------------------------------------------
>
> I've tried using write-warning, but this doesn't go to STDERR.
>
> Mike
>
>

My System SpecsSystem Spec
Old 12-18-2007   #5 (permalink)
Steven Hystad


 
 

Re: Output to STDERR

[Console]::Error.WriteLine("Error Text!")

"Mike Miller" <mike@xxxxxx> wrote in message
news:OG5p4NMQIHA.4752@xxxxxx
Quote:

> Shay, Kiron,
>
> Thanks for giving this a try, but the suggestions you made only simulate
> the look of STDERR output in powershell, but is still printed to STDOUT.
>
> If this was a bash script, I would say:
> echo "This is written to STDERR" 1>&2
>
> I had noticed this syntax in some of the PowerShell docs. Interestingly
> PowerShell will redirect STDERR to STDOUT using this syntax, but throws an
> error when trying to use this method to redirect STDOUT to STDERR.
>
> PS>write-error "I'm redirecting STDERR to STDOUT" 2>&1
> write-error "I'm redirecting STDERR to STDOUT" 2>&1 : I'm redirecting
> STDERR to STDOUT
>
> Or from a script:
> PS>& c:\test2.ps1
> C:\test2.ps1 : I'm redirecting STDERR to STDOUT
> At line:1 char:2
> + & <<<< c:\test2.ps1
>
> But when redirecting STDOUT to STDERR:
> PS>write-output "I'm trying to write to STDERR" 1>&2
> The redirection operator '1>&2' is not supported yet.
> At line:1 char:49
> + write-output "I'm trying to write to STDERR" 1>&2 <<<<
>
> I'd welcome any other ideas. For now I'll use write-stderr. It certainly
> isn't unusable for my case, just noisier than necessary.
>
> Mike
>
>
> "Mike Miller" <mike@xxxxxx> wrote in message
> news:OFTqo2pPIHA.484@xxxxxx
Quote:

>> I'm looking for a way to write to STDERR. The only method I've found so
>> far is write-error, but PowerShell likes to add "useful" information like
>> script name, arguments, etc. How can this be done?
>>
>> To elaborate, I'm building a hook for subversion which will deny anyone
>> from committing too much data at one time. In order to return an error
>> message to the user, the error needs to be written to STDERR. AFAIK the
>> only way to do this is with write-error.
>>
>> So currently, the user gets an error back like this (hyphens mine):
>> ----------------------------------------------------------------------
>> C:\svn\repos\myrepo\hooks\pre-commit.ps1 :
>> Your commit exceeds the maximum size allowed by this server.
>> At line: 1 char: 39
>> + c:\svn\repos\myrepo\hooks\pre-commit.ps1 <<<< c:\svn\repos\myrepo 3-1
>> ----------------------------------------------------------------------
>>
>> Instead of simply:
>> ----------------------------------------------------------------------
>> Your commit exceeds the maximum size allowed by this server.
>> ----------------------------------------------------------------------
>>
>> I've tried using write-warning, but this doesn't go to STDERR.
>>
>> Mike
>>
>>
>
>
My System SpecsSystem Spec
Old 12-18-2007   #6 (permalink)
Mike Miller


 
 

Re: Output to STDERR

Steven, this is PERFECT! Thank you very much.

Mike


"Steven Hystad" <steven.hystad@xxxxxx> wrote in message
news:eeD3$kTQIHA.4740@xxxxxx
Quote:

> [Console]::Error.WriteLine("Error Text!")
>
> "Mike Miller" <mike@xxxxxx> wrote in message
> news:OG5p4NMQIHA.4752@xxxxxx
Quote:

>> Shay, Kiron,
>>
>> Thanks for giving this a try, but the suggestions you made only simulate
>> the look of STDERR output in powershell, but is still printed to STDOUT.
>>
>> If this was a bash script, I would say:
>> echo "This is written to STDERR" 1>&2
>>
>> I had noticed this syntax in some of the PowerShell docs. Interestingly
>> PowerShell will redirect STDERR to STDOUT using this syntax, but throws
>> an error when trying to use this method to redirect STDOUT to STDERR.
>>
>> PS>write-error "I'm redirecting STDERR to STDOUT" 2>&1
>> write-error "I'm redirecting STDERR to STDOUT" 2>&1 : I'm redirecting
>> STDERR to STDOUT
>>
>> Or from a script:
>> PS>& c:\test2.ps1
>> C:\test2.ps1 : I'm redirecting STDERR to STDOUT
>> At line:1 char:2
>> + & <<<< c:\test2.ps1
>>
>> But when redirecting STDOUT to STDERR:
>> PS>write-output "I'm trying to write to STDERR" 1>&2
>> The redirection operator '1>&2' is not supported yet.
>> At line:1 char:49
>> + write-output "I'm trying to write to STDERR" 1>&2 <<<<
>>
>> I'd welcome any other ideas. For now I'll use write-stderr. It certainly
>> isn't unusable for my case, just noisier than necessary.
>>
>> Mike
>>
>>
>> "Mike Miller" <mike@xxxxxx> wrote in message
>> news:OFTqo2pPIHA.484@xxxxxx
Quote:

>>> I'm looking for a way to write to STDERR. The only method I've found so
>>> far is write-error, but PowerShell likes to add "useful" information
>>> like script name, arguments, etc. How can this be done?
>>>
>>> To elaborate, I'm building a hook for subversion which will deny anyone
>>> from committing too much data at one time. In order to return an error
>>> message to the user, the error needs to be written to STDERR. AFAIK the
>>> only way to do this is with write-error.
>>>
>>> So currently, the user gets an error back like this (hyphens mine):
>>> ----------------------------------------------------------------------
>>> C:\svn\repos\myrepo\hooks\pre-commit.ps1 :
>>> Your commit exceeds the maximum size allowed by this server.
>>> At line: 1 char: 39
>>> + c:\svn\repos\myrepo\hooks\pre-commit.ps1 <<<< c:\svn\repos\myrepo 3-1
>>> ----------------------------------------------------------------------
>>>
>>> Instead of simply:
>>> ----------------------------------------------------------------------
>>> Your commit exceeds the maximum size allowed by this server.
>>> ----------------------------------------------------------------------
>>>
>>> I've tried using write-warning, but this doesn't go to STDERR.
>>>
>>> Mike
>>>
>>>
>>
>>
>

My System SpecsSystem Spec
Old 12-19-2007   #7 (permalink)
Karl Prosser[MVP]


 
 

Re: Output to STDERR

Thats actually beautiful way to bypass powershell for this need. Elegant.
My System SpecsSystem Spec
Old 12-19-2007   #8 (permalink)
Kiron


 
 

Re: Output to STDERR

PowerShell has its own version of [Console]::Error.WriteLine(), but I noticed neither behave the same in PowerShell.

Try this:
-< t1.ps1 >-
'hi'
[datetime]'text'
$host.ui.WriteErrorLine('ErrorLine1...')
[Console]::Error.WriteLine('ErrorLine2...')
'bye'
-< t1.ps1 >-

# all stdErr is redirected fine from Cmd.exe
cmd
powershell c:\t1 2> c:\err.txt
type c:\err.txt
exit

# not all stdErr redirected in PowerShell
.\t1 2> c:\err.txt
gc c:\err.txt

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
stderr from external program to write-error PowerShell
Deadlock when logging to stdout and stderr PowerShell
StdOut and StdErr reading... VB Script
Catching stdout AND stderr PowerShell
Help with stderr from native console application 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