为什么我不能从远程运行TestApp得到3行输出?在字符串“输出”中,我获得了许可证信息以及“ Start @”行,但没有接下来的两行:
public static string RunPsExec()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\Users\Vence\Downloads\PSTools\PsExec.exe";
p.StartInfo.Arguments = @"\\10.215.yy.yy -u xxxxxx -p ""xxxxxx"" C:\Projects\TestApp\TestApp\bin\Debug\TestApp.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
p.WaitForExit();
return output;
}
TestApp:
static void Main(string[] args)
{
Console.WriteLine("Start@ " + DateTime.Now.ToShortTimeString());
System.Threading.Thread.Sleep(5000);
Console.WriteLine("Middle@ " + DateTime.Now.ToShortTimeString());
System.Threading.Thread.Sleep(5000);
Console.WriteLine("End@ " + DateTime.Now.ToShortTimeString());
}
答案 0 :(得分:0)
我看到的一个解决方案是将RedirectStandardInput设置为false,因为在此示例中不需要。 但是,如果您希望将来将输入传递给TestApp(要求RedirectStandardInput = true),则一种解决方案可能是使用PAexec(https://www.poweradmin.com/paexec/)替代PsExec。根据这些问题(Can't receive asynchronous output from psexec when launching application remotely),PAExec直接写入控制台缓冲区,而不是stout / sterr。要使其工作,您将必须异步读取输出,如下所示:
public static string RunPsExec()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\<path_to>\PaExec.exe";
p.StartInfo.Arguments = @\\10.215.yy.yy -u xxxxxx -p ""xxxxxx"" ""C:\Projects\TestApp\TestApp\bin\Debug\TestApp.exe"" ";
p.OutputDataReceived += (sender, args) => Display(args.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit(10000);
return;
}
static void Display(string output)
{
Console.WriteLine(output);
}