在控制台应用程序中接收带有等待开关的Get-Content输出

时间:2017-02-15 12:59:47

标签: c# powershell

我试图处理日志文件输出并将其放在绘图上。但是,我无法抓住Get-Content -Wait。 似乎我的C#程序根本没有被调用。没有等待开关就可以正常工作,但这不是我需要的。 简单样本:

using static System.Console;

public class Program
{
    public static void Main(string[] args)
    {
        WriteLine("Starting...");
        if (IsInputRedirected)
        {
            while (In.Peek() != -1)
            {
                Write("[");
                var input = In.ReadLine();
                WriteLine($"{input}]");
            }
            WriteLine("... done.");
        }
        else
        {
            WriteLine("Nothing");
        }
    }
}

使用类似的示例调用:

gc .\Program.cs | .\bin\Debug.ConsoleTest.exe

gc .\Program.cs -Wait | .\bin\Debug.ConsoleTest.exe

有人知道如何从控制台应用程序使用-Wait接收Get-Content的输出吗?

1 个答案:

答案 0 :(得分:0)

Get-content用于获取文件的内容,而不是剥离程序的输出。 你可以使用日志文件中的get-content,但是要知道如果你使用wait,它会等到你杀死procces等待。

所以简而言之Get-content -Wait仅用于跟踪从其他进程写入的日志文件。

只需使用.\Program.cs | .\bin\Debug.ConsoleTest.exe,它就会等到退出。 如果你想要stdout和stderror,你需要一个像Here所描述的结构(请注意使用$stdout = $p.StandardOutput.ReadToEnd() ;$stderr = $p.StandardError.ReadToEnd()进行大输出时出现问题,它们会相互死锁)

相关问题