【问题标题】:What does '$?' command do/mean in powershell? [duplicate]'$?' 是什么意思?命令在PowerShell中做/意味着什么? [复制]
【发布时间】:2015-08-24 10:30:06
【问题描述】:

我知道使用 '$?'在 powershell 中,通过打印 'True' 或 'False' 告诉用户上一个命令是否成功执行。

请求帮助/澄清以下问题:

首先,我想确认一下我在第一段中所说的是否属实。

其次,如果可能的话,关于这个命令的正式文档将非常受欢迎。

第三,如果我对 '$?' 的效用的表述有误? powershell中的命令,那么我想知道它的实际用途是什么。

【问题讨论】:

标签: powershell


【解决方案1】:
  1. 是的,这是真的。

  2. @Kayasax 为您提供了来自 technet.microsoft.com 的官方文档

自己试试吧:

Example (credits to yonglianglee):

? (美元符号 + 问号)返回 True 或 False 值 指示上一个命令是否以错误结束。对于一些 原因是它无法捕获所有错误,但大多数情况下它都能正常工作。

任务 1:查看系统中是否存在 powershell cmdlet。代码。

SomeCmdLet #does not exists
$?
$?

输出

The term 'SomeCmdLet' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ SomeCmdLet <<<<  #does not exists
+ CategoryInfo          : ObjectNotFound: (SomeCmdLet:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
False    #error occured - previous cmdlet (SomeCmdLet) was not found
True     #no errors returned by the previous command ($?)

任务 2:查看系统中是否存在 WMI 类

gwmi win32_processo -ErrorAction SilentlyContinue   #intentional error, win32_processor is the right one
$?
$?

输出:

False
True

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 2013-10-26
    • 2014-07-30
    • 2019-07-11
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多