0
我開始知道使用'$?'在PowerShell中通過打印「True」或「False」告訴用戶前一個命令是否成功執行。
請求幫忙/澄清以下問題,請:
首先,我想證實我在第一段陳述是否屬實。其次,如果可能的話,關於這個命令的正式文檔將非常受歡迎。
第三,如果我錯誤地指出了'$?'的效用,命令在powershell中,那麼我想知道它的實際用途是什麼。
我開始知道使用'$?'在PowerShell中通過打印「True」或「False」告訴用戶前一個命令是否成功執行。
請求幫忙/澄清以下問題,請:
首先,我想證實我在第一段陳述是否屬實。其次,如果可能的話,關於這個命令的正式文檔將非常受歡迎。
第三,如果我錯誤地指出了'$?'的效用,命令在powershell中,那麼我想知道它的實際用途是什麼。
是的,的確如此。
@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
請參閱[get-help about_automatic_variable](https://technet.microsoft.com/en-us/library/hh847768.aspx?f=255&MSPPError=-2147217396) –