I looked and read about problems with psexec.exe from sysinternals that do not work properly with C # and stdout. now I'm trying to figure out how to simply invoke a batch file that has the following, instead of using System.Diagnostics.Process to call psexec:
test.bat contains the following line:
psexec.exe \\hostname -u user -p password ipconfig /all >c:\test.txt
test.txt will be saved on the host where I run my application with sharp and execute psexec.
when i do the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process(); psexec_run.StartInfo.FileName = "cmd.exe"; psexec_run.StartInfo.Arguments = @"/c """ + cur_dir + @"\test\test.bat"""; psexec_run.StartInfo.UseShellExecute = false; psexec_run.Start(); psexec_run.WaitForExit();
I see that the cmd window pops up and it starts something, but I'm not sure that it is leaving.
if I do the following:
System.Diagnostics.Process psexec_run = new System.Diagnostics.Process(); psexec_run.StartInfo.FileName = cur_dir + "\\test\\psexec.exe"; psexec_run.StartInfo.Arguments = @"\\hostname -u user -p password ipconfig /all"; psexec_run.StartInfo.UseShellExecute = false; psexec_run.Start(); psexec_run.WaitForExit();
then I see that the command window is open, and it starts psexec, which takes quite a few seconds, and I quickly see my output, which I need, but I have no way to capture the output or write it to a file.
I think my problem is now, since psexec will not work with stdout, how can I capture the output of the psexec command to write it to a file ???
see the following link for problems with psexec, the last answer to this url mentioned a way to write process output to a file without using stdout, I am new to C #, I canโt figure out how to write process output without using stdout: (
http://forum.sysinternals.com/psexec-fails-to-capture-stdout-when-launched-in-c_topic19333.html
based on the answer below i tried the following:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + "\\test\\psexec.exe", @"\\hostname -u user -p password ipconfig /all"); psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; Process p = Process.Start(psi); StreamReader myStreamReader = p.StandardOutput;
I did ReadToEnd, so I would make sure it got all the output, it DOES NOT DELETE! for some reason this is only the first line of ipconfig output that was it. Also, the cmd window that it opened never closed for some reason. even with CreateNoWindow = true the code just hangs. so again something is wrong with psexec and stdout i think ??? since I can just run this code with the ipconfig / all command on the local host and not use psexec ...
again, I'm looking to avoid stdout and somehow find a way to get the result from this command, or if there isnโt something else, am I already looking? also, so as not to do any more work for anyone, but if you d / l psexec.exe from sysinternals and test it with a command on a remote host, you will see. I spent 2 days on this: (trying to figure out how to use psexec or find some other quick method to execute a remote command on the host and get ouptput.
UPDATE:
I refused psexec in C # code, I saw a lot of messages saying that psexec was eating the result, having a child window process, etcc until my head hurt :), so I'm trying to run a batch file and output it to a file, and it doesn't makes sense ...
I have a batch file test.bat with the following
psexec.exe \\\hostname -u name -p password ipconfig /all >c:\test.txt
when i run the following code:
ProcessStartInfo psi = new ProcessStartInfo(cur_dir + @"\test\test.bat"); psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; Process p = Process.Start(psi); p.WaitForExit();
cmd windows arrive and go very quickly, and the test.txt file is created, but does not contain 0 bytes.
so if i run the batch file on windows command line with psexec command, it works fine !! ???
so there was a problem to check psexec: I changed the batch file to:
ipconfig /all >c:\test.txt
I am executing my code above and it works fine, it creates the result in the test.txt file .. ??? !!!!
Why doesnโt it work with psexec, did I miss something? if it is psexec, does anyone have any recommendations on how i can execute the command on a remote windows host and get output ???