我正在尝试使用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;
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这个时,我得到:
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Error:
Incorrect command line
Run Code Online (Sandbox Code Playgroud)
把它写到屏幕我得到:
C:\Program Files\7-Zip\7z.exe -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
Run Code Online (Sandbox Code Playgroud)
所以我假设我需要在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;
Run Code Online (Sandbox Code Playgroud)
但后来我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
写出来给了我:
"C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
Run Code Online (Sandbox Code Playgroud)
当直接粘贴到命令窗口时,它按预期工作.我一直试图弄清楚这一点,但我想我错过了一些东西(可能很明显).任何人都可以看到我需要做什么才能让这次运行?
Dar*_*te1 52
找到此脚本并根据您的需要进行调整.你能试试吗:
$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
Run Code Online (Sandbox Code Playgroud)
只需在命令前加上 & 号
& "C:\Program Files\7-Zip\7z.exe" -mx=9 a "c:\BackupFolder\backup.zip" "c:\BackupFrom\backMeUp.txt"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70040 次 |
| 最近记录: |