I am kicking off PsExec using a C# .NET 4 process with the 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 fine when I am inside the same domain as my develoment machine (for ALL calls that I am making which is more than just this one I am running into issues with) and when doing simple DOS commands (rmdir and mkdir) cross domain (as I am doing them right before this call).
When I have it run the following command, however:
C:\7Zip\7za x "C:[File Path Includes Spaces]\__STAGING__\App01 [Trunk - STAGE_20121217.2].7z" -o"C:[File Path Includes Spaces]\__STAGING__\" -y -aoa
it never returns. I can see it START to return data (I am redirecting the output using the process and it starts to return what it did), but then it abruptly stops and just doen't do anything. It never returns after the call to
process.WaitForExit();
The odd part is that it DOES sucessfully complete. I can RDP into the server we are extracting the files in and I can see the files there, it's just the PsExec doesn't return anything. I've already tried removing the -y -aoa switches and moving the order around of things and nothing seems to work. FYI, the server we are trying to extract this at is a Windows Server 2003 edition and a Windows Server 2008 R2 edition.
As asked, here is exactly what I am doing 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;
}