0

I'm trying to run automated installations via CMD commands. The programs output progress and I need to capture that output and calculate total progress in a nice window. In my understanding it is impossible to elevate and redirect at the same time. I've tried...

Running cmd elevated and feeding it commands.

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Verb = "runas",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        proc.StandardInput.WriteLine("command");

Running cmd with command as an argument

            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = "/C " + "command",
                    Verb = "runas",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }
            };
            proc.Start();

To no effect. Is there any tricks to this? Elevating after input/output has been captured? I need this to work. Would this be possible with psexec?

Eugene
  • 115
  • 1
  • 4
  • You're trying to run automated installations via a batch program which displays output, capture the output and display in real time... Sorry but why not just rewrite the commands to run via your WPF? In any case, [see this answer](http://stackoverflow.com/a/4501659/612717) – Chibueze Opata Jul 16 '14 at 22:45

1 Answers1

0

If you own the installer programs, you can rewrite them so that they do not use standard streams as a means of communication, but for example named pipes. If you don't, you can write a wrapper program that runs elevated (you run it with UseShellExecute), does not use the standard streams, but runs the installer programs and redirects their input (you run them without UseShellExecute) reporting progress to the nonelevated main program via named pipes or some other means. I hope this diagram makes it clearer:

non-elevated.exe
    \
     \ named pipes
      \
elevated-wrapper.exe (ran with UseShellExecute=true)
        \
         \ redirected standard streams
          \
installer-program.exe (ran with UseShellExecute=false and redirected streams)
cynic
  • 5,305
  • 1
  • 24
  • 40