4

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

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

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

  3. 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).

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
  • does your custom app perhaps run as a different user? Or with a different working directory? (since you don't set that) – Marc Gravell Jul 25 '11 at 06:59
  • No, my custom tool simply outputs some stuff to the console. It does perform impersonation to connect to another system. But all of this works fine when running from command line... – lysergic-acid Jul 25 '11 at 07:00
  • When running psexe from your tool, look in TaskManager under what user it is executed and see if it is the same one as the one you use in your other test scenarios – sternr Jul 25 '11 at 07:06

2 Answers2

4

try adding -accepteula to your arguments to psexec

Sonny
  • 41
  • 1
2

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • the process indeed does not terminate, although i am calling p.Close() at the end of the operation. The remote process (2 processes - PSEXECSVC and my application) also remain running. – lysergic-acid Jul 25 '11 at 07:09
  • When not redirecting output, and using a window and shell to execute command, it is running fine. – lysergic-acid Jul 25 '11 at 07:47