使用Powershell并行解压缩多个文件

时间:2018-06-29 16:57:06

标签: powershell unzip

我正在尝试使用PowerShell实现并行解压缩。附加的代码将一次解压缩一个与关键字匹配的文件。有没有办法我可以并行解压缩多个文件

Code

2 个答案:

答案 0 :(得分:1)

您可以使用将提供并行处理的工作流程 您可以通过https://docs.microsoft.com/en-us/system-center/sma/overview-powershell-workflows?view=sc-sma-1801

了解更多有关工作流程的信息。
workflow Unzip-File{
    Param (
        [Object]$Files,
        [string]$Destination,
        [switch]$SeprateFolders
    )
    foreach –parallel ($File in $Files){
        if($SeprateFolders){
            Write-Output "$($file.Name) : Started"
            Expand-Archive -Path $File -DestinationPath "$Destination\$($file.Name)"
            Write-Output "$($file.Name) : Completed"
        }else{
            Write-Output "$($file.Name) : Started"
            Expand-Archive -Path $File -DestinationPath $Destination
            Write-Output "$($file.Name) : Completed"
        }      
    }
}

try{
    $ZipFiles = Get-ChildItem C:\Users\Default\Desktop\ZipTest\Source\*.zip
    Unzip-File -Files $ZipFiles -Destination "C:\Users\Default\Desktop\ZipTest\Destination" -SeprateFolders
}catch{
    Write-Error $_
}

答案 1 :(得分:0)

我会尝试一下:https://github.com/nightroman/SplitPipeline

$Zipfiles = gci [...]

$ZipFiles | Split-Pipeline -Count 10 {process{ Expand-Archive -Path $_ -DestinationPath [...] }}

您还可以为每个项目开始工作,然后在循环结束时接收它们。 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_jobs?view=powershell-6

相关问题