Powershell System.IO.Compression.ZipFile多个目录

Powershell System.IO.Compression.ZipFile多个目录,powershell,zip,compression,Powershell,Zip,Compression,如果我为我的$destination设置了一个数组,我就可以实现这个功能,但这会为每个选中的目录创建一个单独的.zip 如何选择每个目录并将其压缩到单个目标文件下,而不是多个目标文件下?这是我的密码: $type = "*.txt" $destination = "LogsBackup.zip" Add-Type -Assembly "System.IO.Compression.FileSystem" ; $_sources = dir c:\Logs\$type -Recurse | Se

如果我为我的
$destination
设置了一个数组,我就可以实现这个功能,但这会为每个选中的目录创建一个单独的.zip

如何选择每个目录并将其压缩到单个目标文件下,而不是多个目标文件下?这是我的密码:

$type = "*.txt"
$destination = "LogsBackup.zip"

Add-Type -Assembly "System.IO.Compression.FileSystem" ;

$_sources = dir c:\Logs\$type -Recurse | Select Directory -Unique | 
            Out-GridView -OutputMode Multiple |
            Select @{Name="Path";Expression={$_.Directory -As [string]}} 

for ($i = 0; $i -lt $_sources.Length; $i++)
{
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory( $_sources[$i].Path , $destination)
}

我想保留我的
Out GridView
选项,如果它是一个目录或多个目录,我可以选择它们并将它们存储为数组。

参数
-update
将确保使用从
Out GridView
中选择的新条目更新现有存档

$destination = "C:\logs\LogsBackup.zip"

#Create a single archive
Get-ChildItem -Path C:\logs -Filter *.txt -Recurse | 
    Select @{Name="Path";Expression={$_.DirectoryName}} -Unique  | 
        Out-GridView -PassThru | Compress-Archive -CompressionLevel Optimal -DestinationPath $destination -Update

如果您使用的是Powershell v5,这可以非常容易地完成。是的,它的Powershell v5这几乎正是我想要的,只是如果它不存在,我希望它创建存档。我确定这是一个参数标志,但我不是100%确定要实现这一点。好的,我想我现在可以工作了,但是我如何在gridview中显示不仅仅是目录路径。我还想显示目录内容。它现在可以工作了。我刚刚意识到,并不是所有我将分发此脚本以供我们公司使用的用户都有版本5。第四版有同等版本吗?谢谢!