问题
I am experiencing a weird issue when attempting to run a .NET command line tool remotely using PsExec.
When running PsExec from command line, it runs and completes fine.
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.
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);
}
This code runs fine from cmd line, or from a standalone app!
I have no idea what else could be wrong here.
Our in house tool spawns a new thread and runs this code in it.
Update:
command line + args in command line window -- working. Same cmd + args, run as a Process with RedirectOutput - stalls and returns on timeout.
Could this be a bug in .NET ? (this happens for other progarms, batch files, etc).
回答1:
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).
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).
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.
回答2:
try adding -accepteula to your arguments to psexec
来源:https://stackoverflow.com/questions/6812637/issues-with-running-a-psexec-process-from-code