PowerShell Start-Process WTF

Hi folks

Another WTF… Let’s try

$p = Start-Process -FilePath cmd -ArgumentList "/c exit 123" -PassThru

And it returned nothing. As I described in the previous blogpost, this means that some exception occurred



Exception calling "get_ExitCode" with "0" argument(s): "Process was not started by this object, so requested information cannot be dete rmined."
At line:1 char:16
+ $p.get_ExitCode <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

That is a known PowerShell bug: Start-Process: http://connect.microsoft.com/PowerShell/feedback/details/585549/start-process-passthru-return-value and http://connect.microsoft.com/PowerShell/feedback/details/520554/start-process-does-not-return-exitcode-property

And here is the workaround

$p = [System.Diagnostics.Process]::Start("cmd", "/c exit 123")

returns 123

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s