powershell如何解析命令名称?

Mar*_*lug 6 powershell

我试图找出powershell如何解析名称,但我似乎无法找到任何信息.

这是场景:

存在可执行文件:

c:\stuff\log.exe
Run Code Online (Sandbox Code Playgroud)

路径设置如此 $env:path = 'c:\stuff\'

我加载了一个模块,其中包含一个函数名称"log"和一个别名为"log"的二进制cmdlet.

当我在命令行中键入"log"时,PowerShell如何决定是执行c:\ stuff\log.exe还是函数名称日志,还是将cmdlet作为日志执行?

从实验来看,分辨率顺序似乎是:Cmdlet函数在路径上可执行

但我找不到任何记录这件事的东西.

Ryn*_*ant 11

来自help about_Command_Precedence:

If you do not specify a path, Windows PowerShell uses the following
precedence order when it runs commands:
     1. Alias
     2. Function
     3. Cmdlet
     4. Native Windows commands
Run Code Online (Sandbox Code Playgroud)

也,

When the session contains items of the same type that have the same   
name, such as two cmdlets with the same name, Windows PowerShell      
runs the item that was added to the session most recently.  
Run Code Online (Sandbox Code Playgroud)

调用具有相同名称的命令

about_Command_Precedence 还详细介绍了如何显式调用具有相同名称的命令.

以下是log从不同来源调用命令的一些方法.

# Add '.exe' for executables
log.exe 'This is my log message.'

# Specify the module name
MyModule\log 'This is my log message.'

# Specify alias vs function
&(gi alias:\log) 'This is my log message.'
&(gi function:\log) 'This is my log message.'
Run Code Online (Sandbox Code Playgroud)


Dun*_*can 8

如果您想查找Powershell查找命令的命令,请尝试使用trace-commandcmdlet.例如:

PS C:\scripts> trace-command -name CommandDiscovery -command ls -PSHost
DEBUG: CommandDiscovery Information: 0 : Looking up command: ls
DEBUG: CommandDiscovery Information: 0 : Alias found: ls  Get-ChildItem
DEBUG: CommandDiscovery Information: 0 : Cmdlet found: Get-ChildItem  Microsoft.PowerShell.Commands.GetChildItemCommand


    Directory: C:\scripts


Mode                LastWriteTime     Length Name
...
Run Code Online (Sandbox Code Playgroud)

很好很短,但是:

PS> trace-command -name CommandDiscovery -command log -PSHost
Run Code Online (Sandbox Code Playgroud)

在我的系统上搜索不存在的日志命令时,会产生超过1,000行输出.

该命令似乎基本上是扩展别名然后查找函数,cmdlet然后搜索您的路径以获取该命令,然后使用get-prepended 再次完成所有操作.

语言参考相当简洁,但确实说:

3.8名称查找可以使不同类型的命令具有相同的名称.在这种情况下执行名称查找的顺序是别名,函数,cmdlet和外部命令.

如果它提到当找不到命令时它会再次尝试'get-',我没有找到那个位.