首页 > 解决方案 > 使用powershell并行解压缩多个文件

问题描述

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

代码

标签: powershellunzip

解决方案


您可以使用将提供并行处理的工作流您可以在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 $_
}

推荐阅读