Having issues with psexec more or less systematically hanging on some machines, I decide to give a try to paexec.
It seems to work much better (as far as I have tried) except for one thing: I cannot seem to capture the stderr output from the calling program.
Sample below is short linqpad script. When run using psexec, I get stderr (showing me failure reasons). When I use paexec, stderr remains totally empty.
Can you spot what's wrong? Is there anything special to do or are the "error" messages just written to the console stream and not stderr?
Thanks for info,
Dominique
void Main()
{
var cmdargs =@"\machine c:windowssystem32inetsrvappcmd list apppool";
for(int i=0;i<10;i++) {
Debug.WriteLine("Trial #{0}",i);
psexec(cmdargs, true);
}
}
// ISSUE: if paexec used, nothing from stderr is captured at all (stdout seems ok)
// (with psexec, stderr is captured, but due to other reasons psexec fails)
string pexec() {return "paexec.exe";}
// Define other methods and classes here
void psexec(string cmdargs, bool showOutput=true)
{
// uses method described in http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
// Read at least one stream asynchronously to avoid deadlock condition
StringBuilder outputTxt=new StringBuilder();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName =pexec();
startInfo.Arguments = cmdargs;
Debug.WriteLine(cmdargs);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.OutputDataReceived +=
new DataReceivedEventHandler(
(object proc,DataReceivedEventArgs outline)=>{
if (!String.IsNullOrEmpty(outline.Data))
outputTxt.Append(Environment.NewLine+outline.Data);
});
process.Start();
process.BeginOutputReadLine();
string errTxt=process.StandardError.ReadToEnd();
process.WaitForExit();
Debug.WriteLine("Process exit code: {0}",process.ExitCode);
process.Close();
if (showOutput){
Debug.WriteLine("ERR:"+errTxt);
Debug.WriteLine("OUT:"+outputTxt);
}
}
asked
17 Sep '13, 04:38
ddewaleffe
13●1●2●4
accept rate:
0%