我尝试使用7-Zip备份Powershell(v2)脚本中的某些文件。
我有:
$zipPath = "C:\Program Files\7-Zip\7z.exe"
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""
&$zipPath $zipArgs;
但是当我跑步时,我得到了:
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Error:
Incorrect command line
将此内容写入我得到的屏幕:
C:\Program Files\7-Zip\7z.exe -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
所以我假设我需要在7z.exe的路径周围加上引号,这给了我:
$zipPath = "C:\Program Files\7-Zip\7z.exe"
$zipPath = " `"$zipPath`" "
[Array]$zipArgs = "-mx=9 a", "`"c:\BackupFolder\backup.zip`"", "`"c:\BackupFrom\backMeUp.txt`""
&$zipPath $zipArgs;
但后来我收到以下错误:
The term '"C:\Program Files\7-Zip\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 c
orrect and try again.
At C:\BackupScript\Backup.ps1:45 char:22
+ & <<<< `"$zipPath`" $zipArgs;
+ CategoryInfo : ObjectNotFound: ("C:\Program Files\7-Zip\7z.exe":String) [], CommandNotFound
Exception
+ FullyQualifiedErrorId : CommandNotFoundException
写出来给了我:
"C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
直接粘贴到命令窗口时,可以正常工作。 我一直试图弄清楚这一点,但我想我错过了一些东西(可能很明显)。任何人都可以看到我需要做些什么来实现这个目标?
答案 0 :(得分:44)
找到this脚本并根据您的需要进行调整。你可以尝试一下:
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupFolder\backup.zip"
sz a -mx=9 $Target $Source
答案 1 :(得分:8)
put&#34;&amp;&#34; 7z命令之前的特殊字符。示例:&amp; 7z ...
答案 2 :(得分:2)
也许更简单的解决方案是通过cmd
在您的Powershell上运行7-zip:
cmd /c 7za ...
答案 3 :(得分:0)
尝试使用参数-file指定程序或脚本的位置:
-file&#34; C:\ Program Files \ someting.exe&#34;
答案 4 :(得分:0)
简单地使用&符号后缀命令
& "C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
答案 5 :(得分:0)
如果正确调整了它:不要忘记“ $ Target”上的“” 并避免在c:\ programm文件中的$ 7zipPath路径中留有空格
Set-Alias 7zip $7zipPath
$Source = "c:\BackupFrom\backMeUp.txt"
$Target = "c:\BackupFolder\backup.zip"
7zip a -mx=9 "$Target" "$Source"
或
7z a "$ArchiveName" -t7z '@listfile.txt'