PsExec 1.98 (generated from a C # process) never terminates when starting 7Zip through a domain

I am running PsExec using a C # .NET 4 process with code

var startInfo = new ProcessStartInfo(psExecLocation) { Arguments = string.Format(@"\\{0} -accepteula -u {1} -p {2} {3}", serverName, username, password, command), RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, StandardErrorEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8, UseShellExecute = false }; 

Using PsExec works great when I am in the same domain as my development machine (for ALL calls that I make, which is more than just that I encounter) and when executing simple DOS commands (rmdir and mkdir) cross domain (as I do them right before this call).

If I have this command, run the following command:

C: \ 7Zip \ 7za x "C: [File path includes spaces] \ __ STAGING __ \ App01 [Trunk - STAGE_20121217.2] .zz" -o "C: [File path includes spaces] \ __ STAGING __ \ "-y -aoa

he never returns. I can see that START returns data (I redirect the result using the process, and it starts to return what he did), but then it stops abruptly and just does nothing. He never returns after a call.

 process.WaitForExit(); 

The odd part is that it completed successfully. I can use RDP on the server in which we extract the files, and I see the files there, it just PsExec returns nothing. I already tried to remove the -y-aa switches and move the order around things, and nothing works. FYI, the server we are trying to retrieve, is a version of Windows Server 2003 and a version for Windows Server 2008 R2.

As already mentioned, here is what I do to open the process using PsExec:

  try { using (var process = new Process()) { var startInfo = new ProcessStartInfo(psExecLocation) { Arguments = string.Format(@"\\{0} -accepteula -u {1} -p {2} {3}", serverName, username, password, command), RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, StandardErrorEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8, UseShellExecute = false }; process.StartInfo = startInfo; var processOutput = ""; Action<object, DataReceivedEventArgs> action = (obj, eventArgs) => { if (string.IsNullOrWhiteSpace(eventArgs.Data) == false) processOutput += string.Format("{0}\r\n", eventArgs.Data); }; process.OutputDataReceived += new DataReceivedEventHandler(action); process.ErrorDataReceived += new DataReceivedEventHandler(action); process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); // There appears to be a fault in PsExec that has everything go to error even if it is output. This gets around that output = ""; if (string.IsNullOrWhiteSpace(processOutput) == false) { output = processOutput; // error code 0 means it worked / succeeded. Any other "error code" means something failed var loweredOutput = processOutput.ToLower().Replace("\r\n", "").Trim(); return loweredOutput.EndsWith("error code 0.") || loweredOutput.EndsWith("error code 0"); } // if it got here, psexec didn't return anything. That SHOULD never happen (emphasis on should) return false; } } catch (Exception ex) { Logging.Logger.LogError(ex); output = ""; return false; } 
+4
source share
1 answer

This is actually not the answer, but I ran into very similar problems. I work in the same domain, but I see the same results as described.

Every time I start PsExec from C # code or from PowerShell, the process seems to freeze. When I look at the remote server, I see that the PSEXESVC process is spinning along with the command that I tried to run on the remote server. The team fails, but PSEXESVC seems to be hanging.

The remote command I pass is: cmd /c dir E:\temp

I tested this from LINQPad with setting CreateNoWindow to false along with a redirect set to false, and the command line that opens returns the expected result. As soon as I try to capture the output again, the process will return to a freeze state.

I know this was used to work with C # using the same code a year or 2 ago, so I'm not sure what has changed to make it hang now.

0
source

All Articles