How to capture process output asynchronously in powershell?

后端 未结 5 2069
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 11:10

I want to capture stdout and stderr from a process that I start in a Powershell script and display it asynchronously to the console. I\'ve found some documentation on doing

5条回答
  •  难免孤独
    2020-12-05 11:37

    I couldn't get either of these examples to work with PS 4.0.

    I wanted to run puppet apply from an Octopus Deploy package (via Deploy.ps1) and see the output in "real time" rather than wait for the process to finish (an hour later), so I came up with the following:

    # Deploy.ps1
    
    $procTools = @"
    
    using System;
    using System.Diagnostics;
    
    namespace Proc.Tools
    {
      public static class exec
      {
        public static int runCommand(string executable, string args = "", string cwd = "", string verb = "runas") {
    
          //* Create your Process
          Process process = new Process();
          process.StartInfo.FileName = executable;
          process.StartInfo.UseShellExecute = false;
          process.StartInfo.CreateNoWindow = true;
          process.StartInfo.RedirectStandardOutput = true;
          process.StartInfo.RedirectStandardError = true;
    
          //* Optional process configuration
          if (!String.IsNullOrEmpty(args)) { process.StartInfo.Arguments = args; }
          if (!String.IsNullOrEmpty(cwd)) { process.StartInfo.WorkingDirectory = cwd; }
          if (!String.IsNullOrEmpty(verb)) { process.StartInfo.Verb = verb; }
    
          //* Set your output and error (asynchronous) handlers
          process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
          process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
    
          //* Start process and handlers
          process.Start();
          process.BeginOutputReadLine();
          process.BeginErrorReadLine();
          process.WaitForExit();
    
          //* Return the commands exit code
          return process.ExitCode;
        }
        public static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
          //* Do your stuff with the output (write to console/log/StringBuilder)
          Console.WriteLine(outLine.Data);
        }
      }
    }
    "@
    
    Add-Type -TypeDefinition $procTools -Language CSharp
    
    $puppetApplyRc = [Proc.Tools.exec]::runCommand("ruby", "-S -- puppet apply --test --color false ./manifests/site.pp", "C:\ProgramData\PuppetLabs\code\environments\production");
    
    if ( $puppetApplyRc -eq 0 ) {
      Write-Host "The run succeeded with no changes or failures; the system was already in the desired state."
    } elseif ( $puppetApplyRc -eq 1 ) {
      throw "The run failed; halt"
    } elseif ( $puppetApplyRc -eq 2) {
      Write-Host "The run succeeded, and some resources were changed."
    } elseif ( $puppetApplyRc -eq 4 ) {
      Write-Warning "WARNING: The run succeeded, and some resources failed."
    } elseif ( $puppetApplyRc -eq 6 ) {
      Write-Warning "WARNING: The run succeeded, and included both changes and failures."
    } else {
      throw "Un-recognised return code RC: $puppetApplyRc"
    }
    

    Credit goes to T30 and Stefan Goßner

提交回复
热议问题