powershell - 在远程计算机上运行 Invoke-Command 时出现 OutOfMemory 错误

标签 powershell memory memory-management memory-leaks out-of-memory

$Machines = Get-Content Machines.txt
    foreach($Machine in $Machines)
      {
        $session = New-PSSession -ComputerName $Machine -Credential $creds 
        Invoke-Command -ComputerName $Machine -ScriptBlock {
           #Download 
           & 'wget' 'http://software/software.7z'
           #Extract
           & '7z' 'x' 'C:\software.7z'  '-r' '-y'
           $cleanscript = "Remove-Item C:\software.7z  -Force"
           $cleanscript  | Out-File C:\RemoveZip.ps1 -Encoding ascii
           & "C:\RemoveZip.ps1"
           $script = "CALL C:\software\setup.exe"
           $script  | Out-File C:\software.cmd -Encoding ascii
           & "C:\software.cmd"
       } -Credential $creds 
      }    

运行完 30 台机器后,PowerShell 和运行脚本的机器内存不足。如何避免这种情况?

shell 消耗了正在运行的计算机上的全部 16 GB 内存。诸如超出 shell 内存之类的解决方案可能不起作用,例如OutOfMemory Exception on remote execution using Powershell Invoke-Command

最佳答案

如果您想使用 PowerShell,请编写 PowerShell。

$Machines = Get-Content -Path Machines.txt
ForEach ($Machine in $Machines)
{
    Invoke-Command -ComputerName $Machine -Credential $creds -ScriptBlock {
        #Download 
        Invoke-WebRequest -Uri 'http://software/software.7z' -OutFile 'C:\software.7z'

        #Extract
        Start-Process -FilePath '7z.exe' -ArgumentList 'x','C:\software.7z','-r','-y' -NoNewWindow -Wait
        Remove-Item -Path C:\software.7z -Force

        Start-Process -FilePath 'C:\software\setup.exe'
    }  
}

我怀疑所有远程文件写入和外部调用都可能导致问题(另外,如果您使用的是 v5+,请使用 *Archive cmdlet over 7z)。此外,您正在生成一个 session 但从未使用它。

关于powershell - 在远程计算机上运行 Invoke-Command 时出现 OutOfMemory 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176048/

相关文章:

linux - 从 Expect 捕获返回码

powershell - 在批处理脚本中执行 powershell 命令时转义字符

python - 安全地覆盖 RAM 中的 Python 变量?

c++ - 如何捕获 64 位应用程序上的堆栈损坏?

java - 这个 64 位数字变量多线程规则在 Java 1.8 中仍然适用还是已经过时了?

linux - 我想了解有关 dma_map 和 dma_unmap 的更多信息

.net - 一元运算符 '--' 后缺少表达式

powershell - 从 Powershell 中运行 msdeploy.exe

c - C中的段错误,操作系统如何管理它?

c++ - STL自定义分配器来管理不同的内存空间