【问题标题】:PowerShell does not recognise external executablesPowerShell 无法识别外部可执行文件
【发布时间】:2016-09-06 05:52:08
【问题描述】:

为什么我不能在以下 PowerShell 脚本中运行 7z

Set-Location "C:\Program Files\7-Zip"
Invoke-Expression "dir"
Invoke-Expression "7z"
Invoke-Expression "7z.exe"

结果:

Directory: C:\Program Files\7-Zip


Mode                LastWriteTime     Length Name                                                                                                                        
----                -------------     ------ ----                                                                                                                        
-a---        11/18/2010   9:08 PM      91020 7-zip.chm                                                                                                                   
-a---        11/18/2010   9:08 PM      86016 7-zip.dll                                                                                                                   
-a---        11/18/2010   9:24 PM    1422336 7z.dll                                                                                                                      
-a---        11/18/2010   9:08 PM     284160 7z.exe                                                                                                                      
-a---        11/18/2010   9:27 PM     162816 7z.sfx                                                                                                                      
-a---        11/18/2010   9:27 PM     152064 7zCon.sfx                                                                                                                   
-a---        11/18/2010   9:10 PM     740352 7zFM.exe                                                                                                                    
-a---        11/18/2010   9:11 PM     387072 7zG.exe                                                                                                                     
-a---         9/10/2010  11:41 AM        333 descript.ion                                                                                                                
-a---        11/18/2010   9:11 PM      32400 History.txt                                                                                                                 
-a---          1/2/2010   3:18 PM       1927 License.txt                                                                                                                 
-a---        11/18/2010   9:12 PM       1565 readme.txt                                                                                                                  
7z : The term '7z' 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
+ 7z
+ ~~
    + CategoryInfo          : ObjectNotFound: (7z:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

7z.exe : The term '7z.exe' 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
+ 7z.exe
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (7z.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

  • 7z 在 powershell 中处理起来很麻烦。您需要使用 & 语法。这是让它发挥作用的唯一方法。
  • 查看these uses of & and Start,看看他们是否有助于解决问题
  • 我试过Invoke-Expression "& 7z",但它仍然给了我一个错误。我是否正确使用了&
  • 因为当您在 PowerShell 中的文件夹中时,除非您首先指定 .\ ,否则您无法直接在该文件夹中运行东西,例如.\7z.exe.
  • 谢谢。 Invoke-Expression "./7z" 工作。 .\7z 也有效。

标签: powershell


【解决方案1】:

默认情况下,Powershell 会在 PATH 上查找可执行文件,但不在当前目录 (.\) 中查找。你可以:

  1. 从当前目录调用时,在可执行文件的名称前添加“.\”:

    Invoke-Expression ".\7z"
    
  2. 通过完整路径调用它(使用提到的& 语法):

    & "c:\Program Files\7-zip\7z"
    
  3. 将可执行文件的目录添加到 PATH(Powershell 中的$env:Path):

    $env:Path += "c:\Program Files\7-zip"
    Invoke-Expression "7z"
    

【讨论】:

  • 如果我使用第三种方法,它是临时的(仅在脚本运行时影响该脚本)还是永久的(与在系统变量对话框中添加路径的效果相同)?
  • 它是临时的 - 它仅针对当前进程修改 PATH 变量。如果要使其永久化,请使用[Environment]::SetEnvironmentVariable (technet.microsoft.com/en-us/library/ff730964.aspx)。
猜你喜欢
  • 1970-01-01
  • 2013-08-07
  • 2019-07-30
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多