I am using System.Diagnostics.Process as it allows me to get the error code and associated error.
However, when I set StartInfo.RedirectStandardOutput = $false output is not echoed to my console window, so currently I am forced to add an extra by-ref argument $stdout and echo it from the calling function.
This is not ideal as some commands may generate a huge amount of text, and I am worried about buffer overflows.
Any way that I can still use similar System.Diagnostics.Process code below, still capture errors to string, but let output flow to console normally without redirection to stdout?
function Run([string] $runCommand,[string] $runArguments,[ref] [string] $stderr) { $p = New-Object System.Diagnostics.Process $p.StartInfo = New-Object System.Diagnostics.ProcessStartInfo; $p.StartInfo.FileName = $runCommand $p.StartInfo.Arguments = $runArguments $p.StartInfo.CreateNoWindow = $true $p.StartInfo.RedirectStandardError = $true $p.StartInfo.RedirectStandardOutput = $false $p.StartInfo.UseShellExecute = $false $p.Start() | Out-Null $p.WaitForExit() $code = $p.ExitCode $stderr.value = $p.StandardError.ReadToEnd() # what I have been doing is using a stdout by-ref variable and echoing out contents # $stdout.value = $p.StandardOutput.ReadToEnd() return $code }