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,


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's gravatar image

accept rate: 0%

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 17 Sep '13, 04:38

Seen: 5,742 times

Last updated: 05 Oct '21, 02:21