【问题标题】:I am unable to resolve "Exception of type 'System.OutOfMemoryException' this error我无法解决“'System.OutOfMemoryException' 类型的异常”这个错误
【发布时间】:2021-02-19 11:07:16
【问题描述】:

我正在编写一个 powershell 脚本以在 C:\ 驱动器中大小为 47 GB 的远程计算机上创建一个 zip 文件夹。该文件夹也在 C:\ drive 中。 C:\ 驱动器有 66gb 可用空间,但仍然给我“没有足够的内存资源”的错误来创建 zip,所以我尝试创建从 C:\ 到 H: 的 zip。我的 H:\ 驱动器有 121 GB 可用空间,但仍然给我错误。 我附上了这两个错误的屏幕截图。

$curentDirectory = $PSScriptRoot
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
            Write-Output "Script Current Directory is:"$curentDirectory
            Write-Output "Reading Configuration From :"$curentDirectory"/config.xml"

            $myData = [xml](Get-Content $curentDirectory"/config.xml" -ErrorAction Stop)
            $machineName = $myData.'tangoEnvironment'.VM01.machineName
            $machineDrive= $myData.'tangoEnvironment'.VM01.machineDrive
            $remoteUser = $myData.'tangoEnvironment'.VM01.remoteUser
            $remotePassword = $myData.'tangoEnvironment'.VM01.remotePassword
            $userCred     =     New-Object Management.Automation.PSCredential($remoteUser, (convertto- 
                    securestring $remotePassword -asplaintext -force))
                    Invoke-Command -ComputerName $machineName -Credential $userCred -ScriptBlock {
                        
                        Write-Host    ""   
                        Write-Host     "Response from Jump Server($env:COMPUTERNAME) ..."
                        # Rest of service
                        Compress-Archive -Path C:/Humera -DestinationPath H:"/Humera.zip" 
                        
                    }

   cmd /c pause

【问题讨论】:

  • ZIP 对存档的大小和存档内的文件有限制。尝试改用 7-zip。
  • 磁盘空间不是这里的问题。 Invoke-Command 表示代码在远程 shell 中运行,并且 shell 可以分配多少内存(RAM,而不是磁盘空间)会有限制。
  • 欢迎来到 Stack Overflow。除了上述两个有效点之外,ZIP 在工作区中工作,并根据该工作区写入其输出。在某些情况下,它需要至少 100% 的额外空间才能实现其目标。

标签: powershell zip powershell-remoting


【解决方案1】:

Powershell 远程会话的内存限制很小 - 请参阅 the answers to this question:

来自: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384372(v=vs.85).aspx - 远程 shell 的默认内存限制为 150MB

要解决,可以增加这个:

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1000000
Set-Item WSMan:\localhost\Plugin\Microsoft.PowerShell\Quotas\MaxMemoryPerShellMB 1000000
Restart-Service WinRM

但是Compress-Archive也有大小限制!

Compress-Archive cmdlet 使用 Microsoft .NET API System.IO.Compression.ZipArchive 压缩文件。最大文件 大小为 2 GB,因为存在底层 API 的限制。

您需要使用 7zip 之类的第三方实用程序来压缩这么大的单个文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多