【问题标题】:How does powershell resolve command names?powershell如何解析命令名?
【发布时间】:2014-03-07 19:51:45
【问题描述】:

我试图弄清楚 powershell 如何解析名称,但我似乎找不到任何信息。

这是场景:

存在一个可执行文件:

c:\stuff\log.exe

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

我加载了一个包含函数名称“log”和别名为“log”的二进制 cmdlet 的模块。

当我在命令行输入“log”时,PowerShell如何决定是执行c:\stuff\log.exe还是函数名log,还是别名为log的cmdlet?

从实验看来,解决顺序是: cmdlet 功能 路径上的可执行文件

但我找不到任何可以证明这一点的东西。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    来自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
    

    还有,

    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.  
    

    调用同名命令

    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.'
    

    【讨论】:

      【解决方案2】:

      补充现有的优秀答案:

      一种简单实用的确定给定命令名称将执行的方法是否存在其他具有相同名称的隐藏命令

      Get-Command -All <commandName>
      

      具有给定名称的所有命令将按优先级降序排列,即,有效命令将在第一个列出。

      例如,Windows PowerShell 为Set-Content cmdlet 提供了一个内置的sc 别名,它会隐藏本机sc.exe(服务控制)程序(除非您将其调用为sc.exe):

      PS> Get-Command -All sc
      
      CommandType     Name                                               Version    Source                                                                                                           
      -----------     ----                                               -------    ------                                                                                                           
      Alias           sc -> Set-Content                                                                                                                                                              
      Application     sc.exe                                             10.0.17... C:\WINDOWS\system32\sc.exe                                                                                       
      

      【讨论】:

        【解决方案3】:

        如果您想了解 Powershell 查找命令的顺序,请尝试使用 trace-command cmdlet。例如:

        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
        ...
        

        又好又短,但是:

        PS> trace-command -name CommandDiscovery -command log -PSHost
        

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

        顺序似乎基本上是扩展别名然后查找函数,cmdlet 然后在您的路径中搜索命令,然后在前面加上get- 再次执行所有操作。

        语言参考在这方面相当简洁,但它确实说:

        3.8 名称查找 不同种类的命令可能都具有相同的名称。名称查找的执行顺序 这种情况是别名、函数、cmdlet 和外部命令。

        如果它提到当没有找到命令时,它会在前面加上“get-”再次尝试,我还没有找到那个位。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-27
          • 1970-01-01
          • 2016-03-24
          相关资源
          最近更新 更多