1

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 } 

1 Answer 1

2

You may not need to use the System.Diagnostics.Process object. Just execute the EXE and scoop up the info like this:

$stdout = .\foo.exe 2> fooerr.txt Get-Content fooerr.txt return $LastExitCode 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.