.NET控制台输出和PSExec

本文关键字:PSExec 输出 控制台 NET | 更新日期: 2023-09-27 18:25:08

我正在运行PSExec-Microsoft工具,Process类执行远程命令,其输出如下:

            Process p = new Process();
            string args = @"''remotemachine -u someuser -p somepass wmic product get name";
            ProcessStartInfo ps = new ProcessStartInfo();
            ps.Arguments = args;
            ps.FileName = psExecFileName;
            ps.UseShellExecute = false;
            ps.CreateNoWindow = true;
            ps.RedirectStandardOutput = true;
            ps.RedirectStandardError = true;
            p.StartInfo = ps;
            p.Start();
            StreamReader output = p.StandardOutput;
            string output = output.ReadToEnd();

其中wmic产品get-name是远程运行的WMI工具,它自己的输出列出了远程计算机上所有安装的应用程序。因此,在输出中,我看不到wmic的输出,同时当我在命令行中本地运行PSExec时,我可以完全看到PSExec和远程启动wmic的输出。问题是,如何在本地机器上捕获所有输出?我应该在一个单独的控制台中运行它,并尝试连接到控制台以捕获所有输出吗?

更一般地说,如果简单地说,为什么直接运行PSExec时,流程StandardOutput和控制台中的输出不一样?

.NET控制台输出和PSExec

ReadToEnd将等待进程退出。例如,psExecFile中的Console.ReadLine()可能会阻止您的读取。但你可以得到已经写好的流,

            StreamReader output = p.StandardOutput;
            string line;
            while ((line = output.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }

在控制台中,写入StandardOutputStandardError的数据显示在控制台中。

在你的程序中,你需要单独看待每一个。。。试着在末尾添加这样的内容:

string error = p.StandardError.ReadToEnd();