这个问题在这里已经有了答案:
What is `$?` in Powershell?
(4 个回答)
6年前关闭。
我开始知道使用'$?在 powershell 中,通过打印“True”或“False”来告诉用户上一个命令是否成功执行。
请就以下问题寻求帮助/澄清:
首先,我想确认我在第一段中所说的是否属实。
其次,如果可能的话,关于这个命令的正式文档将非常受欢迎。
第三,如果我对“$”的效用的表述有误? powershell 中的命令,那么我想知道它的实际用途是什么。
最佳答案
自己尝试一下:
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/