什么是"约会"?"Get-Command date"抛出CommandNotFoundException

mat*_*tt9 14 powershell

date 工作:

PS> date

Saturday, June 10, 2017 9:10:11 AM
Run Code Online (Sandbox Code Playgroud)

Get-Command date抛出异常:

PS> Get-Command date
Get-Command : The term 'date' 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:1
+ Get-Command date
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (date:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Run Code Online (Sandbox Code Playgroud)

Get-Alias date抛出异常:

PS> Get-Alias date
Get-Alias : This command cannot find a matching alias because an alias with the name 'date' does not exist.
At line:1 char:1
+ Get-Alias date
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (date:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand
Run Code Online (Sandbox Code Playgroud)

$PSVersionTable.PSVersion 在Windows 10上是"5.1.15063.296".

date似乎不是cmdlet,函数,脚本文件,运行程序或别名.那么,是什么date

Mat*_*sen 16

PowerShell CommandDiscovery API使用以下优先顺序将命令名解析为命令:

  1. 别号
  2. 功能
  3. 小命令
  4. 本机Windows命令

如果命令名中没有包含一个破折号或斜线,并用尽最后一个选项列表上方后没有命令已被发现,它会再次尝试,但Get-预谋.

因此,Get动词也称为默认命令动词.

这适用于任何Get-*与现有别名,函数或cmdlet名称或本机可执行文件没有冲突的命令$env:path,例如:

Alias
Item
ChildItem
Run Code Online (Sandbox Code Playgroud)

... 等等.


如果您再次想知道为什么命令以某种方式得到解决,您可以使用Trace-Commandcmdlet从powershell引擎获取调试级别信息:

Trace-Command -Expression { date } -Name Command* -Option All -PSHost
Run Code Online (Sandbox Code Playgroud)

Command*在这种情况下,将匹配CommandSearchCommandDiscovery例程,并准确显示powershell用于解析命令名称的步骤date

  • 感谢您快速详细的解答!多亏了这一点,我明白了'date`对于PowerShell来说是一个未知的术语,但是,对于"Get-prepended"功能,调用了`get-date`. (2认同)

mkl*_*nt0 7

为了补充Mathias R. Jessen的有用答案,它很好地解释了命令发现过程:

Get-Command是不知道默认动词逻辑,因此不报告当使用给定命令名时实际执行的相同命令是应该修复不一致,因为这是给定命令名时的目的.

  • 顺便说一句:该-All选项可以让你看到同一个名字的其他命令以较低的优先级,但即使机智-All date没有找到(如果是这样,它应该被列为第一,符合报告它作为有效的命令,没有-All).

同样,Get-Help并且-?也不知道默认动词逻辑:

  • date -?并且Get-Help date不回复Get-Date's帮助; 相反,他们列出包含该单词的 帮助主题date.

在PowerShell GitHub存储库中创建了一个问题.

  • 尼斯.看看源代码,看起来像`Get-Command`实际上正确地调用了CommandDiscovery类,但是省略了来自输出端的已解决命令,可能是开发者方面的疏忽/遗忘:-P (3认同)