powershell - "OutOfMemoryException"对 60Gb 文件使用压缩归档时

标签 powershell powershell-5.0

我正在尝试使用 Compress-Archive 压缩 IIS 日志文件(总共 60GB) ,但是我收到错误:

"Exception of type 'System.OutOfMemoryException' was thrown."


$exclude = Get-ChildItem -Path . | Sort-Object -Descending | Select-Object -First 1
$ArchiveContents = Get-ChildItem -Path . -Exclude $exclude | Sort-Object -Descending
Compress-Archive -Path $ArchiveContents -DestinationPath .\W3SVC2.zip -Force

我已经调整好了 MaxMemoryPerShellMB2048MB并重新启动 WinRM 服务。

RAM 执行命令时内存消耗超过 6GB。

最佳答案

正如马特所建议的那样,我建议将其归档为文件集。就像是:

For($i=0;$i -le $ArchiveContents.Count;$i=$i+10){
    Compress-Archive -Path $ArchiveContents[$i..($i+9)] -DestinationPath .\W3SVC2-$i.zip -Force
}

这样您一次只能处理 10 个文件。您可能想要调整文件的数量,具体取决于您拥有的文件数量和大小,即:如果您有 300 个文件,每个文件大小为 200MB,那么一次 10 个可能会很好,而如果您有 3000 个文件,每个文件大小为 20MB每个你可能想要增加到 50 甚至 100。

编辑:我刚看了,Compress-Archive支持通过指定 -Update 将文件添加到存档中范围。您应该能够做与上面相同的事情,稍作修改,以制作 1 个大文件。
For($i=0;$i -le $ArchiveContents.Count;$i=$i+10){
    Compress-Archive -Path $ArchiveContents[$i..($i+9)] -DestinationPath .\W3SVC2.zip -Update
}

这应该一次向目标存档添加 10 个文件,而不是尝试一次添加所有文件。

关于powershell - "OutOfMemoryException"对 60Gb 文件使用压缩归档时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616028/

相关文章:

powershell - 将用户输入转换为日期时间值

windows - 在 Windows 10 中使用 powershell 在多个桌面上启动程序

powershell - 如何从Powershell中的字符串转换为UInt64?字符串到数字的转换

powershell - 无法在 ScriptBlock 中访问函数

xml - PowerShell:如何将InnerText值连接为单个字符串?

powershell - 为什么 PowerShell 脚本在提示输入时会暂停?

powershell - powerShell Set-acl奇怪的错误: there is not enough space on the disk

mysql - 结果集是 1 条记录的对象,多条记录的数组?

powershell - 从Cygwin运行脚本时,Powershell Read-Host cmdlet导致挂起

loops - 一个循环中有两个变量