4
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = @"PsExec.exe";
p.StartInfo.Arguments = @"\\10.10.1.127 -accepteula -i -u administrator -p 1 -n 10 c:\myapp.exe";
p.Start();

How can I determine whether p.ExitCode is the code coming from PsExec or myapp.exe?

Pablo
  • 28,133
  • 34
  • 125
  • 215
  • 1
    Assuming that myapp.exe is your own app, you can try giving your own return code like: `Environment.Exit(-2345);` So if the error code is coming from your myapp.exe, it should give the same exit code to you( i.e. -2345). – Anshuman Chatterjee Sep 18 '15 at 12:21
  • 1
    PsExec is using Windows System error codes. I may also need to use the same range in `myapp`. – Pablo Sep 18 '15 at 12:22
  • According to [this](https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) it looks like it's your app's return code _Error codes returned by PsExec are specific to the applications you execute, not PsExec._ – juharr Sep 18 '15 at 12:27
  • In case PsExec succeeded to run it's my application exit code, in case of failed then PsExec's error code. But how to distinguish? – Pablo Sep 18 '15 at 12:29
  • The only way to do that is make sure the codes don't overlap. – juharr Sep 18 '15 at 12:30
  • That was my fear too... – Pablo Sep 18 '15 at 12:31
  • PsExec should fail when the PsExec.exe is not found or the arguments are erroneous. You may check the exit codes for these known scenarios, rest all are from your app. Also, you may always code your myapp.exe to produce the return code between a certain range. – Anshuman Chatterjee Sep 18 '15 at 12:31
  • or host not reachable, or DCOM is misconfigured, or.. too many of them to consider each separately. – Pablo Sep 18 '15 at 12:33
  • It looks like all Windows System error codes are positive based on [this](https://msdn.microsoft.com/en-us/library/ms681381.aspx). So you chould return negative values, then you'd know those are from your app and then negate them to map them to the appropriate Widnows System error. – juharr Sep 18 '15 at 12:39
  • PsTools returns an error message as the last line of output when it encounters an error (host not found, logon failure, command file not found). Could you use a combination of this and the non-zero error code? – Ian Sep 18 '15 at 13:15
  • @Ian: If that's the only way then I can do tricky things. – Pablo Sep 18 '15 at 13:32
  • @Pablo It's certainly not a tidy solution, but it's a solution! – Ian Sep 18 '15 at 14:01

1 Answers1

0

Use a combination of non-zero error code and parsing of the last couple of lines of output from PsExec.

Ian
  • 1,221
  • 1
  • 18
  • 30