我正尝试在 Powershell (v2) 脚本中使用 7-Zip 来备份一些文件。
我有:
$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"
如果直接粘贴到命令窗口中,它可以正常工作。 我已经尝试了一段时间,但是我想我可能错过了什么(很可能很明显的东西)。 有人可以看到我需要做什么才能使其运行吗?
& "C:\Program Files\7-Zip\7z.exe" "-mx=9" a C:\BackupFolder\Backup.zip C:\BackupFrom\backMeUp.txt
。 - Bill_Stewart"-mx=9 a"
,但我需要的是"-mx=9" a
。我无法使用$zipArgs变量使其工作,但可以使用& "$zipPath" "-mx=9" a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
。 - IGGt&$zipPath @zipArgs
。 - groser