【问题标题】:From PowerShell, how can the location of an executable be resolved?从 PowerShell 中,如何解析可执行文件的位置?
【发布时间】:2021-09-08 21:55:23
【问题描述】:

在很多情况下,我需要一个可执行文件或命令行工具的路径,例如:notepadkubectl。在使用 PowerShell 时,可执行文件是可用的,但文件的物理位置并不总是很容易找到。

一种方法是搜索 PATH 上的每个文件夹,甚至更糟糕的是系统 (gci -r | % { $_ ...}),但这并不是最有效的时间利用方式,每次都重新编码。有没有更好的办法?

【问题讨论】:

    标签: powershell file path executable


    【解决方案1】:

    Get-Command 将返回一个对象,该对象包含多个带有路径名的字段。例如,如果我在我的系统上输入Get-Command notepad,我会得到

    PS Z:\> Get-Command notepad
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Application     notepad.exe                                        10.0.18... C:\WINDOWS\system32\notepad.exe
    
    

    如果我输入 Get-Command notepad | Select-Object * 我会得到

    PS Z:\> Get-Command notepad | Select-Object *                               
                                                                                
                                                                                
    HelpUri            :                                                        
    FileVersionInfo    : File:             C:\WINDOWS\system32\notepad.exe      
                         InternalName:     Notepad                              
                         OriginalFilename: NOTEPAD.EXE.MUI                      
                         FileVersion:      10.0.18362.1 (WinBuild.160101.0800)  
                         FileDescription:  Notepad                              
                         Product:          Microsoft® Windows® Operating System 
                         ProductVersion:   10.0.18362.1                         
                         Debug:            False                                
                         Patched:          False                                
                         PreRelease:       False                                
                         PrivateBuild:     False                                
                         SpecialBuild:     False                                
                         Language:         English (United States)              
                                                                                
    Path               : C:\WINDOWS\system32\notepad.exe                        
    Extension          : .exe                                                   
    Definition         : C:\WINDOWS\system32\notepad.exe                        
    Source             : C:\WINDOWS\system32\notepad.exe                        
    Version            : 10.0.18362.1316                                        
    Visibility         : Public                                                 
    OutputType         : {System.String}                                        
    Name               : notepad.exe                                            
    CommandType        : Application                                            
    ModuleName         :                                                        
    Module             :                                                        
    RemotingCapability : PowerShell                                             
    Parameters         :                                                        
    ParameterSets      :                                                        
                                                                                
    

    【讨论】:

      【解决方案2】:

      我最近发现 Where.exe 可用于 PowerShell。

      > Where.exe kubectl
      C:\Program Files\Docker\Docker\resources\bin\kubectl.exe
      

      这与 PowerShell 变量兼容,因此可以在编写脚本时使用: $path = where.exe notepad

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-13
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多