从code运行PSEXEC过程中的问题 [英] Issues with running a PsExec process from code

查看:192
本文介绍了从code运行PSEXEC过程中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,尝试使用PSEXEC远程运行.NET命令行工具的时候。

I am experiencing a weird issue when attempting to run a .NET command line tool remotely using PsExec.

  1. 在命令行运行PSEXEC,它运行并完成良好。

  1. When running PsExec from command line, it runs and completes fine.

当从一个控制台应用程序运行它(创建进程,     运行PsExec.exe必要的参数吧) - 这是     运行确定。

When running it from a console application (creating a process, running PsExec.exe with the necessary arguments to it) -- it is running OK.

当我们在内部的自定义工具,运行它     用于运行不同的任务,它超时或被不     成功完成。

When running it from our in house custom tool that is used to run different tasks, it either times out or does not complete successfully.

下面是code我使用:

Here is the code i am using:

Process p = new Process();

p.StartInfo.FileName = @"C:\PsExec.exe";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

string arg = "-snapshot -display C:\*.msi -s";

p.StartInfo.Arguments = @"\\10.161.203.106 -u user -p pwd -cf C:\FVT.exe " + arg;

Logger.Info(this, "Starting process");

p.Start();
var ended = p.WaitForExit(60 * 1000);

if (!ended)
{
    throw new Exception("Process timed out.");
}

Logger.Info(this, "Process ended");

using (StreamReader sr = p.StandardOutput)
{
    string buffer = sr.ReadToEnd();
    Logger.Info(this, buffer);
}

这code运行良好的CMD线,或者从一个独立的应用程序!

This code runs fine from cmd line, or from a standalone app!

我不知道还有什么在这里可能是错的。

I have no idea what else could be wrong here.

我们的内部工具生成一个新的线程在运行此code。

Our in house tool spawns a new thread and runs this code in it.

更新:

命令行+的args在命令行窗口 - 工作。 同样CMD +的args,运行与RedirectOutput一个过程 - 摊位和回报超时

command line + args in command line window -- working. Same cmd + args, run as a Process with RedirectOutput - stalls and returns on timeout.

难道这是在.NET中的错误? (这种情况对于其他progarms,批处理文件等)。

Could this be a bug in .NET ? (this happens for other progarms, batch files, etc).

推荐答案

我不知道错误的的,但我必须说,如果你重定向标准错误( RedirectStandardError = TRUE ),读stderr流(像你这样做有标准输出),它会告诉你。另外,在调试时离开 CreateNoWindow = FALSE ,也许你会看到的控制台消息(特别是当它等待一个按键preSS,否则可能会消失得太快通知)。

I don't know what the error is, but I have a hunch that if you redirect stderr (RedirectStandardError = true) and read the stderr stream (like you do with stdout) it will tell you. Alternatively, while debugging leave CreateNoWindow = false and maybe you'll see the console message (especially if it is waiting for a keypress; otherwise it might disappear too quickly to notice).

请注意,你可能需要设置异步读取器在标准输出/标准错误,如果这个过程没有结束。你可以做到这一点无论是在额外的线程,或者通过 OutputDataReceived / ErrorDataReceived 事件(你需要设置 EnableRaisingEvents 也)。

Note that you might need to set up async readers on stdout/stderr if the process isn't terminating. You can do that either on extra threads, or via the OutputDataReceived / ErrorDataReceived events (you need to set EnableRaisingEvents to true also).

如果说的还是的不工作;你可以尝试使用 UseShellExecute = TRUE 运行。这意味着你将不能够重定向IO,所以你可能需要使用> / >> 等管道输出到文件(最好在温度),然后读取该文件。

If that still doesn't work; you could try running with UseShellExecute=true. This means you won't be able to redirect IO, so you might have to use > / >> etc to pipe the output to a file (ideally in temp), then read the file.

这篇关于从code运行PSEXEC过程中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆