powershell - '$?' 命令在 Powershell 中的作用/含义是什么?

标签 powershell

这个问题在这里已经有了答案:





What is `$?` in Powershell?

(4 个回答)


6年前关闭。




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

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

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

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

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

最佳答案

  • 对,是真的。
  • @Kayasax 为您提供了来自 technet.microsoft.com
  • 的官方文档

    自己尝试一下:
    enter image description here

    Example (credits to yonglianglee) :

    ? (dollar sign + question mark) Returns True or False value indicating whether previous command ended with an error. For some reason it does not catch all errors, but most of the time it works.

    Task 1: See if a powershell cmdlet exists in the system. Code.


    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 ($?)
    

    Task 2: See if a WMI class exists in the system


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

    输出:
    False
    True
    

    关于powershell - '$?' 命令在 Powershell 中的作用/含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32180074/

    相关文章:

    windows - 按顺序递归重命名所有子文件夹中的文件

    powershell - 如何从 Powershell 设置 TeamCity 构建状态?

    powershell - Powershell 文件路径错误中的非法字符

    powershell - 获取位置 :\

    powershell - 安装后导入 PowerShell 模块

    powershell - 在Powershell中将嵌套的哈希表导出到CSV

    sql-server - 如何通过SQL Powershell在所有存储过程中搜索字符串?

    powershell - 找不到位置参数

    PowerShell 代码以交互方式运行,但不在脚本内运行

    powershell - 如何在 Team Build 2013 Post-Test 脚本中检测测试运行是否成功?