2016-05-11 7 views
2

次のPowerShellスクリプトで7zを実行できないのはなぜですか?PowerShellで外部実行ファイルが認識されない

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 
+1

7zはPowerShellで扱う面倒です。 &構文を使用する必要があります。それを機能させる唯一の方法です。 – user1751825

+0

[これらの使い方と使い方](http://stackoverflow.com/questions/4639894/executing-an-exe-file-using-a-powershell-script)をチェックし、問題解決に役立つかどうかを確認してください – JGreenwell

+0

I 'Invoke-Expression"&7z "'を試しましたが、それでも私にはエラーが出ました。私は '&'を正しく使いましたか? –

答えて

3

、PowerShellはPATH上ではなく、現在のディレクトリ(.\)に実行ファイルを探しありません。あなたは可能性:現在のディレクトリから呼び出す実行可能ファイルの名前に

  1. Preppend "\。":

    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" 
    
+0

3つ目の方法を使用する場合は、一時的なもの(スクリプトの実行中にそのスクリプトにのみ影響します)か、永続的なもの(システム変数ダイアログにパスを追加するのと同じ効果)ですか? –

+0

一時的です - 現在のプロセスのPATH変数のみを変更します。永続化したい場合は、 '[Environment] :: SetEnvironmentVariable'(https://technet.microsoft.com/en-us/library/ff730964.aspx)を使います。 – qbik

関連する問題