Note that there are some explanatory texts on larger screens.

plurals
  1. PO$LastExitCode=0 but $?=False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
    primarykey
    data
    text
    <p>Why does Powershell show the surprising behaviour in the second example below?</p> <p>First, an example of sane behaviour:</p> <pre><code>PS C:\&gt; &amp; cmd /c "echo Hello from standard error 1&gt;&amp;2"; echo "`$LastExitCode=$LastExitCode and `$?=$?" Hello from standard error $LastExitCode=0 and $?=True </code></pre> <p>No surprises. I print a message to standard error (using <code>cmd</code>'s <code>echo</code>). I inspect the variables <code>$?</code> and <code>$LastExitCode</code>. They equal to True and 0 respectively, as expected. </p> <p>However, if I ask PowerShell to redirect standard error to standard output over the first command, I get a NativeCommandError:</p> <pre><code>PS C:\&gt; &amp; cmd /c "echo Hello from standard error 1&gt;&amp;2" 2&gt;&amp;1; echo "`$LastExitCode=$LastExitCode and `$?=$?" cmd.exe : Hello from standard error At line:1 char:4 + cmd &lt;&lt;&lt;&lt; /c "echo Hello from standard error 1&gt;&amp;2" 2&gt;&amp;1; echo "`$LastExitCode=$LastExitCode and `$?=$?" + CategoryInfo : NotSpecified: (Hello from standard error :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError $LastExitCode=0 and $?=False </code></pre> <p>My first question, why the NativeCommandError?</p> <p>Secondly, why is <code>$?</code> False when <code>cmd</code> ran successfully and <code>$LastExitCode</code> is 0? PowerShell's documentation <a href="http://technet.microsoft.com/en-us/library/dd347675.aspx">about automatic variables</a> doesn't explicitly define <code>$?</code>. I always supposed it is True if and only if <code>$LastExitCode</code> is 0, but my example contradicts that. </p> <hr> <p>Here's how I came across this behaviour in the real-world (simplified). It really is FUBAR. I was calling one PowerShell script from another. The inner script:</p> <pre><code>cmd /c "echo Hello from standard error 1&gt;&amp;2" if (! $?) { echo "Job failed. Sending email.." exit 1 } # Do something else </code></pre> <p>Running this simply as <code>.\job.ps1</code>, it works fine, and no email is sent. However, I was calling it from another PowerShell script, logging to a file <code>.\job.ps1 2&gt;&amp;1 &gt; log.txt</code>. In this case, an email is sent! What you do outside the script with the error stream affects the internal behaviour of the script. <em>Observing a phenomenon changes the outcome.</em> This feels like quantum physics rather than scripting!</p> <p>[Interestingly: <code>.\job.ps1 2&gt;&amp;1</code> may or not blow up depending on where you run it]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload