$ LastExitCode = 0,但$?= False在PowerShell。将stderr重定向到stdout会产生NativeCommandError [英] $LastExitCode=0 but $?=False in PowerShell. Redirecting stderr to stdout gives NativeCommandError
问题描述
为什么Powershell在下面的第二个例子中显示出惊人的行为?
Why does Powershell show the surprising behaviour in the second example below?
首先,一个合理行为的例子:
First, an example of sane behaviour:
PS C:\> & cmd /c "echo Hello from standard error 1>&2"; echo "`$LastExitCode=$LastExitCode and `$?=$?"
Hello from standard error
$LastExitCode=0 and $?=True
没有惊喜。我打印一个消息到标准错误(使用 cmd
的 echo
)。我检查变量 $?
和 $ LastExitCode
。它们分别等于True和0,如预期。
No surprises. I print a message to standard error (using cmd
's echo
). I inspect the variables $?
and $LastExitCode
. They equal to True and 0 respectively, as expected.
但是,如果我要求PowerShell通过第一个命令将标准错误重定向到标准输出,我会得到一个NativeCommandError:
However, if I ask PowerShell to redirect standard error to standard output over the first command, I get a NativeCommandError:
PS C:\> & cmd /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
cmd.exe : Hello from standard error
At line:1 char:4
+ cmd <<<< /c "echo Hello from standard error 1>&2" 2>&1; echo "`$LastExitCode=$LastExitCode and `$?=$?"
+ CategoryInfo : NotSpecified: (Hello from standard error :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
$LastExitCode=0 and $?=False
我第一个问题,为什么是NativeCommandError?
My first question, why the NativeCommandError?
为什么 $?
在 cmd
成功运行且 $ LastExitCode
是0? PowerShell的文档关于自动变量没有明确定义 $?
。我总是认为它是真的,如果且仅当 $ LastExitCode
是0,但我的例子矛盾。
Secondly, why is $?
False when cmd
ran successfully and $LastExitCode
is 0? PowerShell's documentation about automatic variables doesn't explicitly define $?
. I always supposed it is True if and only if $LastExitCode
is 0, but my example contradicts that.
这是我在现实世界中遇到的这种行为(简化)。它真的是FUBAR。我从另一个调用一个PowerShell脚本。内部脚本:
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:
cmd /c "echo Hello from standard error 1>&2"
if (! $?)
{
echo "Job failed. Sending email.."
exit 1
}
# Do something else
运行这个只是作为 .\job.ps1
,它工作正常,电子邮件。但是,我是从另一个PowerShell脚本调用它,日志记录到一个文件。\ job.ps1 2>& 1> log.txt
。在这种情况下,将发送电子邮件!在脚本外使用错误流做什么会影响脚本的内部行为。
Running this simply as .\job.ps1
, it works fine, and no email is sent. However, I was calling it from another PowerShell script, logging to a file .\job.ps1 2>&1 > log.txt
. In this case, an email is sent! What you do outside the script with the error stream affects the internal behaviour of the script. Observing a phenomenon changes the outcome. This feels like quantum physics rather than scripting!
[有趣的是:。\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
[Interestingly: .\job.ps1 2>&1
may or not blow up depending on where you run it]
推荐答案
PS> nslookup microsoft.com 2>&1 ; echo $?
False
这篇关于$ LastExitCode = 0,但$?= False在PowerShell。将stderr重定向到stdout会产生NativeCommandError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!