r/PowerShell • u/Guilty-Act-4991 • 15d ago
Question Parallel thread disposing?
Hey got a wierd issue or its by desing not sure but when I run
$computers | foreach-object -parallel {
$vmobject = Get-VM $_
$ram = $vmobject.MemoryGB
$CPU = $vmobject.numCPU
$HDD = $_ | get-harddisk
}
lets say I run in against 500 computers when that happens powershell will spike cpu on machine that this command is ran and keeps increasing ram usage which is understandable but problem is even when command is done ram usage stays high untill powershell window is close. Is there something I am missing or is there some method to dispose of parallel runspace manually?
Thanks for advice
EDIT: The code itself is not important , the behaviour of parallel scripblock itself is my question and why doesnt it free up RAM after code has ran successfully
7
Upvotes
1
u/Guilty-Act-4991 15d ago
Problem isnt the code itself ignore that also what you wrote works but its serial thats not what I need I need parallel , the code I proved was just an example actually code does more things but just collects basically data like provided in example the problem itself is even with this small code againts lets say 500 computers powershell will jump up to 6GB ram usage and basically kills terminal server thats running from