$之间的区别? 和$ LastExitCode在PowerShell中 $之间的区别? 和$

2019-05-14 08:52发布

在PowerShell中,之间有什么区别$?$LastExitCode

我读了有关自动变量 ,它说:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode Contains the exit code of the last Windows-based program that was run.

在定义$? 这并不能说明什么成功和失败的意思。


我问,因为我假定$? 是真当且仅当$ LastExitCode是0,但我发现一个令人惊讶的反例: $ LastExitCode = 0,但$ =在PowerShell中假? 重定向错误输出到标准输出给NativeCommandError

Answer 1:

$LastExitCode是本机应用程序的返回码。 $? 刚刚返回TrueFalse取决于最后的命令(cmdlet或本地)是否退出没有错误或没有。

cmdlet的失败通常意味着一个例外,本机应用程序是一个非零退出代码:

PS> cmd /c "exit 5"
PS> $?
False
PS> cmd /c "exit 0"
PS> $?
True

取消用Ctrl + C小命令也将计为失败; 本机应用程序这取决于他们建立什么样的退出代码。



文章来源: Difference between $? and $LastExitCode in PowerShell