Running 7-Zip from within a Powershell script

后端 未结 6 669
栀梦
栀梦 2020-12-09 08:10

I\'m trying to use 7-Zip to backup some files inside a Powershell (v2) script.

I have:

$zipPath = \"C:\\Program Files\\7-Zip\\7z.exe\"
[Array]$zipArg         


        
相关标签:
6条回答
  • 2020-12-09 08:10

    Maybe a simpler solution is to run 7-zip on your Powershell via cmd:

    cmd /c 7za ...
    
    0 讨论(0)
  • 2020-12-09 08:13

    If you adapt it correctly: Dont forget the "" on "$Target" and avoid $7zipPath in c:\programm files with a space in the path

    Set-Alias 7zip $7zipPath
    
    $Source = "c:\BackupFrom\backMeUp.txt"
    $Target = "c:\BackupFolder\backup.zip"
    
    7zip a -mx=9 "$Target" "$Source"
    

    or

    7z a "$ArchiveName" -t7z '@listfile.txt'
    
    0 讨论(0)
  • 2020-12-09 08:19

    put "&" special character before 7z command. Example: &7z ...

    0 讨论(0)
  • 2020-12-09 08:28

    Simply prefix the command with an ampersand

    & "C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
    
    0 讨论(0)
  • 2020-12-09 08:37

    Found this script and adapted it to your needs. Can you please try:

    $7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
    
    if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
        throw "7 zip file '$7zipPath' not found"
    }
    
    Set-Alias 7zip $7zipPath
    
    $Source = "c:\BackupFrom\backMeUp.txt"
    $Target = "c:\BackupFolder\backup.zip"
    
    7zip a -mx=9 $Target $Source
    
    0 讨论(0)
  • 2020-12-09 08:37

    try to use parameter -file to specify the location of program or script:

    -file "C:\Program Files\someting.exe"

    0 讨论(0)
提交回复
热议问题