Process.Start does not output the full output

1.4k views Asked by At

I have a process P, which calls the PSEXEC executable, and passes it an argument. My issue is that the output from my C# program is not the same from when I call exactly the same command directly into a command prompt.

Here is the output I expect (when ran from CMD):

Server Name           Server Load
--------------------  ------------
601CTXD04             0
601CTXD05             0
...
601CTXP03             0

And here is the actual output (when ran from my program):

Server Name           Server Load
--------------------  ------------
601CTXD04             0

So when I run the command from my C# program, I only get 1 server. I call the process the following way :

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = cServer.psexec; //Path to the PSEXEC executable
p.StartInfo.Arguments = "-accepteula \\\\601ctxp01 qfarm /load /continue";
p.Start();

string output = p.StandardOutput.ReadToEnd();
string[] lines = Regex.Split(output, "\\r\\n");

I really do not understand why, the exact same command ran from CMD does not give the same output when ran from a C# process. Please let me know what I am doing wrong as I am currently oblivious to my mistake.

3

There are 3 answers

0
MrRoy On BEST ANSWER

It seems the issue was with PsExec itself (a Sysinternal utility) as I have tried an alternative called PAExec and the same command works fine, so I am using this one in my code instead.

2
James Ralston On

Be sure to have the.

p.WaitForExit() 

method after the line where you read the output. Here is an explanation from MSDN: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx

11
Simon Taylor On

You need to call p.WaitForExit() after p.StandardOutput.ReadToEnd(), otherwise you won't get all of the output from the process. Don't put it directly after p.Start() - you may get a deadlock. See http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput%28v=vs.110%29.aspx