Problems with stdout and psexec.exe from sysinternals

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; // Read the standard output of the spawned process. string sOutput = myStreamReader.ReadToEnd(); 

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 ???

+6
source share
5 answers

I found a solution. obviously psexec will NOT work with sharp. so I came up with some wmi code to connect to the remote host and it works EXCELLENT !!! :)

I used microsoft WMICodeCreator.exe to create wmi code for C # for the process method on the remote host, wow, this tool is great because wmi code is a bit confusing to me.

+1
source

I had exactly the same problem. I was getting "windows ip configuration." as the first line when I start psexec. I tried with paexec to work. I used the Marius code.

Note: if you do not use the first cmd / c command in the arguments, the command runs only on the local computer, even if you define the target as \\ remoteserver

  ProcessStartInfo psi = new ProcessStartInfo("cmd.exe"); psi.Arguments = @"cmd/c C:\paexec.exe \\\192.168.2.5 -s -u test.local\administrator -p Password1 cmd /c ipconfig"; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; Process p = Process.Start(psi); System.IO.StreamReader myStreamReader1 = p.StandardOutput; p.WaitForExit(); string sOutput = myStreamReader1.ReadToEnd(); 
+2
source

Are you sure your source code is correct? this link is quite old .. maybe itโ€™s fixed!

Here is an example of how to redirect standard output and output all output to a string via streamreader:

  ProcessStartInfo psi = new ProcessStartInfo("tftp.exe"); // preferences for tftp process psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; Process p = Process.Start(psi); StreamReader myStreamReader = p.StandardOutput; p.WaitForExit(); // Read the standard output of the spawned process. string sOutput = myStreamReader.ReadToEnd(); 
+1
source

I have an answer to this problem that worked for me.

Hope someone finds this helpful.

I literally just spent the last two hours tearing my hair out with this. The psexec tool works completely fine using the normal command line, but when you try to redirect threads, it truncates the output, and you get only half the output.

In the end, how I fixed my problem was a bit of a hack. I passed the command output to a text file and read it back to return it from the function.

I also need to set UseShellExecute to true. Without this, it still wonโ€™t work. This had an unfortunate side effect showing a console window. To get around this, I set the window style to be hidden and hey presto it works !!!

Here is my code:

 string ExecutePSExec(string command) { string result = ""; try { string location = AppDomain.CurrentDomain.BaseDirectory; // append output to file at the end of this string: string cmdWithFileOutput = string.Format("{0} >{1}temp.log 2>&1", command,location ); // The flag /c tells "cmd" to execute what follows and exit System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmdWithFileOutput); procStartInfo.UseShellExecute = true; // have to set shell execute to true to procStartInfo.CreateNoWindow = true; procStartInfo.WindowStyle = ProcessWindowStyle.Hidden; // as a window will be created set the window style to be hiddem System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); proc.WaitForExit(); // now read file back. string filePath = string.Format("{0}temp.log", location); result = System.IO.File.ReadAllText(filePath); } catch (Exception objException) { // Log the exception } return result; } 

and its use:

 string command = @"psexec.exe -l -u domain\username -p password /accepteula \\192.168.1.3 netstat -a -n"; ExecutePSExec(command); 
+1
source

psexec output goes to StandardError, not StandardOutput. I do not know why it is so. The following code snippet will access it.

 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.RedirectStandardError = true; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); errors = process.StandardError.ReadToEnd(); process.Close(); 
0
source

Source: https://habr.com/ru/post/893593/


All Articles