我可以看到已经回答了类似的问题,但似乎没有任何答案可以满足我正在寻找的答案,我尝试使用解决方案/修改它们以满足我的需要,但无法获得正确的输出。
我的主要问题是:你能否将 Powershell 变量传递到 7zip 的命令行。
目前我有脚本:
##########Alias Setting##########
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"
############################################
#### Variables
$filePath = "D:\Logs\test\201310\" #location to look in
$Archive = "D:\Logs\Test\201310\"
$Extension = "*.log" #extensions to look for
$Days = "30" #Number of days past today's date that will be archived
$CutDay = [DateTime]::Now.AddDays($Days)
$log = Get-ChildItem $filepath -include $Extension -recurse | Where-Object {$_.LastWriteTime -lt $CutDay}
########### END of VARABLES ##################
foreach ($File in $log) #current gui output - temp only using for debugging purpose if required and to confirm files.
{
write-host "File Name : $File " $File.LastWriteTime
}
foreach ($file in $log) # actually zipping
{
$archivename = $_.CutDay
sz a -t7z $archivename $archivename -m0=PPMd
}
########### END OF SCRIPT ##########
我想要的是让 7zip 命令行从变量中读取文件名和目的地,我计划尝试将其作为尽可能通用的脚本,以便 $archivename 将取决于文件夹名称和当前日期。
我希望脚本能够通过参数读取它正在压缩的位置,但目前我无法让它读取变量!
如有任何帮助,我们将不胜感激,或者如果您需要澄清,请告诉我。
最佳答案
我发现将参数传递给可执行文件的最佳方法是使用 splatting parameter (@变量名
):
$7zipPath = "$env:ProgramFiles\7-Zip\7z.exe"
$parameter = @('a', '-t7z', '-m0=PPMd', "$target", "$source")
&$7zipPath @parameter
sz a -t7z $archivename $archivename -m0=PPMd
有两个 $archivename
变量传递给 7zip。第一个应该是目标(例如 c:\test.zip
),第二个应该是源(例如 c:\test.text
)
关于powershell - 从 powershell 将变量传递给 7zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31050045/