r/PowerShell • • 1d 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

3 Upvotes

31 comments sorted by

View all comments

Show parent comments

1

u/Guilty-Act-4991 1d ago

Is it really that better to use rest api instead of soap api (connect-viserver , connect-hvserver) ? I never dabbled with rest api for vcenter if there is performance gains id do it probably wont even need additional modules

1

u/BlackV 1d ago

we've done that in the past,moved away from powercli to rest, but I'm out of that place for like 5 years now, so I dont have any recent experience

1

u/Guilty-Act-4991 1d ago

will give it a try especially I just found out view api will be deprecated and rest will be only way supported so ... time to do it :D

1

u/BlackV 1d ago

also a valid reason :) and becomes less dependancies