.NET Framework - Changes to stdout, stderr in PowerShell 2.0

Asked By John Vottero
22-Jan-10 02:48 PM
How did PowerShell's handling of stdout and stderr with native commands
change in v2.0?

We have an application that intercepts stdout and stderr by doing:

Console.SetError(new StreamWriter(ourStreamHandler));
Console.SetOut(new StreamWriter(ourStreamHandler));

then we create a Runspace and Pipeline and execute a PowerShell script.

This sent anything written to stdout or stderr to our stream handler and we
could fool with it before sending on on the the original stderr or stdout.
This worked fine with PowerShell V1.0. although, PowerShell would intercept
anything written to stderr and write a NativeCommandError to the Error
pipeline.

With PowerShell v2.0, output from PowerShell commands is interepted as
before but, when native commands write to stdout, it goes directly to the
original stdout, bypassing our interception.  And, output to stderr is just
sent to stderr (and properly intercepted), a NativeCommandError is not
written to the Error pipeline.

Are these changes documented?  How can I intercept the output from native
commands?
Console.SetError
(1)
Console.SetOut
(1)
NativeCommandError
(1)
OurStreamHandler
(1)
StreamWriter
(1)
Runspace
(1)
Intercepts
(1)
Stdout
(1)
  Marco Shaw [MVP] replied to John Vottero
22-Jan-10 03:23 PM
Hi John,

I cannot help much more than suggest you take a peak here:
http://www.nivot.org/2009/08/19/PowerShell20AConfigurableAndFlexibleScriptLoggerModule.aspx

Marco
Create New Account
help
new StreamWriter("logfile.txt"); } catch(IOException ioe) { Console.WriteLine(ioe.Message + " cannot open file"); return; } Console.SetOut(fileout); Console.WriteLine("Beginning of the file"); for(int i = 0;i<10;i++) Console you Regards Raj C# Discussions Console.OpenStandardOutput (1) IOException (1) OpenStandardOutput (1) Console.WriteLine (1) Console.SetOut (1) StreamWriter (1) et put you might just (R)ead (T)he code in the help (F)ile under console.setout. (M)an! Hi, You can either save the value of Console.Out before changing it dear! you might just (R)ead (T)he code in the help (F)ile under console.setout. (M)an! This how I did: using System; using System.IO; class test { public static
dllimport stdout gets eaten .NET Framework I changed the stdout in my C# app using Console.SetOut. It works fine for all my Console.Out.Write calls and with log4net. However, I keywords: dllimport, stdout, gets, eaten description: I changed the stdout in my C# app using Console.SetOut. It works fine for all my Console.Out.Write calls and with log4net. However, I
Ich wuerde gerne mit Console::WriteLine Debugausgaben im Debugfenster sehen. Dazu müsste ich wahrscheinlich mit Console::SetOut() das Ziel einstellen. Wie bekomme ich aber ein StreamWriter vom Debugfenster der IDE ? Thomas VC Ich wuerde gerne mit Console::WriteLine Debugausgaben im Debugfenster sehen. Dazu müsste ich wahrscheinlich mit Console::SetOut() das Ziel einstellen.