PowerShell STDOUT Formatting Issue
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开发者_如何学Python 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 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?
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.
精彩评论