在 Powershell 脚本中运行 7-Zip [英] Running 7-Zip from within a Powershell script
问题描述
我正在尝试使用 7-Zip 来备份 Powershell (v2) 脚本中的一些文件.
I'm trying to use 7-Zip to backup some files inside a Powershell (v2) script.
我有:
$zipPath = "C:Program Files7-Zip7z.exe"
[Array]$zipArgs = "-mx=9 a", "`"c:BackupFolderackup.zip`"", "`"c:BackupFromackMeUp.txt`""
&$zipPath $zipArgs;
但是当我运行这个时,我得到:
But when I run this I get:
7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
Error:
Incorrect command line
把这个写到屏幕上我得到:
Writing this to the screen I get:
C:Program Files7-Zip7z.exe -mx=9 a "c:BackupFolderackup.zip" "c:BackupFromackMeUp.txt"
所以我假设我需要在 7z.exe 的路径周围加上引号,这给了我:
So I assumed that I needed to put quotes around the path to 7z.exe, that gave me:
$zipPath = "C:Program Files7-Zip7z.exe"
$zipPath = " `"$zipPath`" "
[Array]$zipArgs = "-mx=9 a", "`"c:BackupFolderackup.zip`"", "`"c:BackupFromackMeUp.txt`""
&$zipPath $zipArgs;
但是我收到以下错误:
The term '"C:Program Files7-Zip7z.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:BackupScriptBackup.ps1:45 char:22
+ & <<<< `"$zipPath`" $zipArgs;
+ CategoryInfo : ObjectNotFound: ("C:Program Files7-Zip7z.exe":String) [], CommandNotFound
Exception
+ FullyQualifiedErrorId : CommandNotFoundException
写出来给我:
"C:Program Files7-Zip7z.exe" -mx=9 a "c:BackupFolderackup.zip" "c:BackupFromackMeUp.txt"
直接粘贴到命令窗口时,它按预期工作.我一直试图弄清楚这一点,但假设我遗漏了一些东西(可能很明显).任何人都可以看到我需要做什么才能运行此程序吗?
Which works as expected when pasting straight into a command window. I have been trying to figure this out for a while, but assume I am missing something (probably quite obvious). Can anybody see what I need to do to make this run?
推荐答案
Found this 脚本并根据您的需要进行调整.你能试试吗:
Found this script and adapted it to your needs. Can you please try:
$7zipPath = "$env:ProgramFiles7-Zip7z.exe"
if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
throw "7 zip file '$7zipPath' not found"
}
Set-Alias 7zip $7zipPath
$Source = "c:BackupFromackMeUp.txt"
$Target = "c:BackupFolderackup.zip"
7zip a -mx=9 $Target $Source
这篇关于在 Powershell 脚本中运行 7-Zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!