tags:

views:

338

answers:

2

Is there a way to capture STDOUT information from an executable within powershell, without affecting the default behaviour of the STDOUT process? Whenever I attempt to capture output from an executable within PowerShell, it appears that the STDOUT is then formatted before being returned to the host/screen. Further, it also appears that PowerShell, when asked to capture stdout output, feeds the data to the host line by line, only returning a line of data once a line feed/carriage break is found in the output.

Basically, I want to have a way to capture STDOUT data from an executable run within powershell, without having the STDOUT output, or action, changed IN ANY WAY. Does anyone know if this is possible currently?

I have already read, and I am aware of, the limitations around the Start-Transcript/Stop-Transcript cmdlets. If this was able to grab this output for me, my problem would have been solved.

Thanks in advance to anyone that can assist here.

-M

A: 

I don't know if tee-object would help you?

It's one way to divert the output, but I don't know if it addresses your concerns.

John Weldon
The tee-object's pretty good, but I did notice that it's not possible to append to a file using it, so you need to work around that limitation.
Damien Ryan
I already tried the tee-object; this displays the same symptoms listed in my original post.
Michael S.
+1  A: 

A simple little C# program shows that PowerShell doesn't wait for a line terminator to write output to stdout. Compile this and run it:

using System;
using System.Threading;

class App 
{
    static void Main(string[] args)
    {
        string str = "Hello world!";
        Array.ForEach(str.ToCharArray(), 
                      ch => { Console.Write(ch);Thread.Sleep(500); });
        Console.WriteLine();
        Console.Write("Line start ");
        Thread.Sleep(2000);
        Console.WriteLine(" line term.");
    } 
}

This EXE shows each char of Hello world! appearing on the screen every 500 millisecs. The output appears before the line terminator is written. It is my understanding based on conversations with the PowerShell team that console applications, when run without redirection, get a console handle so they can write directly to stdout bypassing PowerShell and any PowerShell formatting. This was done to allow certain console applications like edit.com to write colored output to the screen.

What is the EXE you are seeing the weird output behavior from? Is it possible you are seeing the effects of console buffering? Does the EXE output behave better when run under CMD.exe?

Keith Hill
Echoing a value in a string variable is not the same thing as what I'm doing. I'm running an Exchange 2007 executable, and attempting to capture the output from the command as it executes, without affecting the default behaviour of the STDOUT functionality/display.
Michael S.
Somewhere in the implmentation of the Exchange Server 2007 exe that you are using, it is "echoing" as you say, bytes to stdout that appear as text on the PowerShell console. The code above compiles to an EXE and shows that PowerShell doesn't wait for a line term. Perhaps you can elaborate a bit more on what exactly you are seeing. For instance, you say you "attempt to capture output" - do you mean you are using file redirection e.g. foo.exe > foo.txt? If so, then you shouldn't see stdout output on the host/screen.
Keith Hill
And if you are seeing output in that case, it is probably stderr output.
Keith Hill