这里有一个非常简单的例子,说明了您想要做什么,从函数的上下文中删除。它假设您的目标文件夹已经存在(您可以使用
Test-Path
和
New-Item
如果没有,则创建它们),并且您正在使用7z.exe。
$directories = @("www","sql","services")
$archiveType = "-tzip"
foreach($dir in $directories)
{
# Use $dir to update the destination each loop to prevent overwrites!
$sourceFilePath = "mySourcePath\$dir"
$destinationFilePath = "myTargetPath\$dir"
cmd /c "$pathToZipExe a $archiveType $destinationFilePath $sourceFilePath"
}
总的来说,看起来您已经非常接近一个解决方案,需要进行一些小的更改来支持foreach循环。如果你有信心
create-7zip
适用于单个文件夹,您可以将其替换为
cmd /c
上面的行。这里有一些方便的清单
example usages
用于命令行上的7zip。