c# psexec response get params

137 views Asked by At

im trying to execute remote process using psexec in c# application. this is my code :

          System.Diagnostics.Process process = new System.Diagnostics.Process();
           System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();


           startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
           startInfo.FileName = "cmd.exe";
           startInfo.Arguments = "/C psexec \\\\"+hostname +" "+command;
           startInfo.RedirectStandardOutput = true;
           startInfo.RedirectStandardError = true;
           startInfo.UseShellExecute = false;
           process.StartInfo = startInfo;
           process.Start();
           response = process.StandardOutput.ReadToEnd();
           process.WaitForExit();

the problem is im not getting response only if command="", i get the definition of the psexec command in response but if command="dir" for exemple i get nothing in response . any help.

1

There are 1 answers

0
usr On BEST ANSWER

You forgot to read standard error as well. Maybe, psexec writes to that.

I suggest you use one of the top voted snippets of stack overflow, to be found by: site:stackoverflow.com process RedirectStandardOutput. There are hundreds of such questions. Most solutions are subtly wrong but certainly better than this one which is clearly incorrect.

This is a good checklist.