Is there a bug in PowerShell's Start-Process
command when accessing the StandardError
and StandardOutput
properties?
If I run the following I get no output:
$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait
$process.StandardOutput
$process.StandardError
But if I redirect the output to a file I get the expected result:
$process = Start-Process -FilePath ping -ArgumentList localhost -NoNewWindow -PassThru -Wait -RedirectStandardOutput stdout.txt -RedirectStandardError stderr.txt
I also had this issue and ended up using Andy's code to create a function to clean things up when multiple commands need to be run.
It'll return stderr, stdout, and exit codes as objects. One thing to note: the function won't accept
.\
in the path; full paths must be used.Here's how to use it:
Here is my version of function that is returning standard System.Diagnostics.Process with 3 new properties
That's how
Start-Process
was designed for some reason. Here's a way to get it without sending to file:IMPORTANT:
We have been using the function as provided above by LPG.
However, this contains a bug you might encounter when you start a process that generates a lot of output. Due to this you might end up with a deadlock when using this function. Instead use the adapted version below:
Further information on this issue can be found at MSDN:
In the code given in the question, I think that reading the ExitCode property of the initiation variable should work.
Note that (as in your example) you need to add the
-PassThru
and-Wait
parameters (this caught me out for a while).I really had troubles with those examples from Andy Arismendi and from LPG. You should always use:
before calling
A full example is: