powershell - Powershell 中的 `$?` 是什么?

标签 powershell

Powershell 中 $ 的含义是什么?

<小时/>

编辑:TechNet answers同义反复,没有解释“成功”或“失败”的含义。

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

我猜$?会简单地测试 $LastExitCode 是否为 0,但我发现了 counter example哪里$?为 False 但 $LastExitCode 为 True。

最佳答案

如果最后一个命令成功,则返回 true,否则返回 false

但是,有许多警告和非显而易见的行为(例如“成功”的确切含义)。我强烈推荐阅读this article以获得更全面的治疗。

例如,考虑调用 Get-ChildItem。

PS> Get-ChildItem 

PS> $? 
    True

$?当调用 Get-ChildItem 成功时,将返回 True

但是,如果您在不存在的目录上调用 Get-ChildItem,则会返回错误。

PS> Get-ChildItem \Some\Directory\Which\Does\Not\Exist
    Get-ChildItem : Cannot find path 'C:\Some\Directory\Which\Does\Not\Exist' because it does not exist.

PS> $?
    False

$?此处将返回 False,因为上一个命令未成功。

关于powershell - Powershell 中的 `$?` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634115/

相关文章:

amazon-web-services - cloudformation将参数传递给ssm文档

PowerShell类型加速器: PSObject vs PSCustomObject

azure - 用于列出 Azure 订阅下虚拟机上次重新启动的 Powershell 脚本

powershell - 在 PowerShell 中使用分隔符连接部件的最佳方法

powershell/sqlplus 错误 SP2-0042 : unknown command " ■@" - rest of line ignored

azure - coverlet 不会在 azure devops 中创建报告

powershell - PowerShell压缩消息字符串输出

c# - 如何从 .Net 程序运行 Import-PSSessions

windows - 在 64 位 Windows 上运行 cURL

powershell - robocopy/MIR - 不要删除目标中的 desktop.ini