PowerShell中的"退出"究竟是什么?

Joe*_*oey 64 powershell

您可以通过键入来退出PowerShell exit.到现在为止还挺好.但究竟是什么呢?

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
Run Code Online (Sandbox Code Playgroud)

所以它既不是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
Run Code Online (Sandbox Code Playgroud)

还有更多这样的命令没有命令吗?

ETA:仅供参考:我知道我可以简单地将其包装成一个函数.我的个人资料有线条

# exit with Ctrl+D
iex "function $([char]4) { exit }"
Run Code Online (Sandbox Code Playgroud)

在他们中.我的问题只是要知道这个命令到底是什么.

zda*_*dan 90

它是一个保留关键字(如return,filter,function,break).

参考

另外,根据Bruce Payette的Powershell in Action第7.6.4节:

但是当您希望脚本从该脚本中定义的函数中退出时会发生什么?...为了使这更容易,Powershell有exit关键字.

当然,正如其他人所指出的那样,通过在函数中包含exit来做你想要的并不难:

PS C:\> function ex{exit}
PS C:\> new-alias ^D ex
Run Code Online (Sandbox Code Playgroud)

  • 根据`Get-Help Exit-PSSession` _您还可以使用Exit关键字结束交互式会话.效果与使用Exit-PSSession相同.所以它也说`Exit`是一个关键字.但是,似乎引用不能暗示`Exit`和`Exit-PSSession`在所有情况下都是等效的.exit关键字可以与一个可以转换为`[int]`的参数一起使用,在这种情况下,该数字将是退出代码.例如`退出42'. (4认同)
  • 我不得不说,这个答案完全是错误的。我在 $profile 中放置了一个函数,在该函数中,我在特定条件下放置了“退出”,它不会退出该函数,而是退出整个控制台。我已经浪费了相当多的时间来搜索什么可以在不终止控制台会话的情况下留下一个函数,我被难住了。以上肯定是错误的。 (2认同)