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; }