gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:38:42 25 4
gpt4 key购买 nike

$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而不是 7z)。此外,您正在生成 session ,但从未使用过它。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com