$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/