2009-08-13 70 views
49

您可以通過輸入exit來退出PowerShell。到現在爲止還挺好。但究竟是什麼呢?PowerShell中的「退出」究竟是什麼?

PS Home:\> gcm exit 
Get-Command : The term 'exit' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch 
eck the spelling of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:4 
+ gcm <<<< exit 
    + CategoryInfo   : ObjectNotFound: (exit:String) [Get-Command], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand 

因此,它既不是cmdlet,函數,腳本或程序。它留下的問題究竟是什麼

這不幸也意味着,一個不能創建別名exit

PS Home:\> New-Alias ^D exit 
PS Home:\> ^D 
Cannot resolve alias '♦' because it refers to term 'exit', which is not recognized as a cmdlet, function, operable prog 
ram, or script file. Verify the term and try again. 
At line:1 char:2 
+ ♦ <<<< 
    + CategoryInfo   : ObjectNotFound: (♦:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : AliasNotResolvedException 

是否有任何這是沒有命令更多這樣的命令?

ETA:僅供參考:我知道我可以簡單地將它包裝成函數。我的個人資料有行

# exit with Ctrl+D 
iex "function $([char]4) { exit }" 

其中。我的問題只是要知道這個命令是什麼。

回答

65

它是一個保留關鍵字(如return,filter,function,break)。

Reference

而且,按照布魯斯·帕耶特的Powershell in Action第7.6.4:

但是當你想要一個腳本從該腳本中定義一個函數中退出時會發生什麼? ...爲了使這更容易,Powershell有退出關鍵字

。當然,其他人指出,這不是很難用一個函數包裝出口做你想做什麼:

PS C:\> function ex{exit} 
PS C:\> new-alias ^D ex 
+3

據'獲取幫助退出-PSSession' _You也可以使用Exit關鍵字來結束交互式會話。效果和使用Exit-PSSession._一樣。所以它也說'Exit'是一個關鍵字。但是,看起來這個引用並不意味着'exit'和'exit-PSSession'在所有情況下都是等價的。 exit關鍵字可以與可以轉換爲'[int]'的參數一起使用,在這種情況下,該數字將是退出代碼。例如'出口42'。 – 2015-10-14 09:20:24