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使用以下优先顺序将命令名解析为命令:
如果命令名中没有包含一个破折号或斜线,并用尽最后一个选项列表上方后没有命令已被发现,它会再次尝试,但有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*在这种情况下,将匹配CommandSearch和CommandDiscovery例程,并准确显示powershell用于解析命令名称的步骤date
为了补充Mathias R. Jessen的有用答案,它很好地解释了命令发现过程:
这Get-Command是不知道默认动词逻辑,因此不报告当使用给定命令名时实际执行的相同命令是应该修复的不一致,因为这是给定命令名时的目的.
-All选项可以让你看到同一个名字的其他命令以较低的优先级,但即使机智-All date没有找到(如果是这样,它应该被列为第一,符合报告它作为有效的命令,没有-All).同样,Get-Help并且-?也不知道默认动词逻辑:
date -?并且Get-Help date不回复Get-Date's帮助; 相反,他们列出了包含该单词的 帮助主题date.我在PowerShell GitHub存储库中创建了一个问题.
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |