Running out of Memory

by notarat at 2013-03-15 11:26:15

I’m trying, unsuccessfully, to pull a listing of computers (supplied from a file) and the groups to which they are associated. The list contains 4500 computers.

Add-PSSnapin Quest.ActiveRoles.ADManagement
$computerlist = Get-Content c:\inputfiles\BIGList.txt
foreach($computer in $computerlist) {
$C=Get-QADcomputer -identity $computer -IncludeAllProperties
$strGC=($C.MemberOf).count
$strGRP=($C.MemberOf | foreach {($.Split(‘,’))[0].Substring(3)}) -Join ","
$strDONE="$C,$strGC,$strGRP"
Add-Content c:\outputfiles\ComputersandGroups.txt -value $strdone
}


When I get to about the 1900th computer on the list, I get an error about running out of memory. My workstation has 16GB and I’m running 64 Bit Win 7 Enterprise.

Checking Task Manager when powershell ISE is opened, it’s taking about 39MB with nothing running. My script has been running now for about 10-11 minutes and it’s memory usage has already climbed to 819MB and is rising.

I thought that maybe the script is not releasing the memory after putting values into the variables and writing them to a file, so I added some lines to the script to clear out the variables after the data is written to the file (See Below)


$computerlist = Get-Content c:\inputfiles\BIGList.txt
foreach($computer in $computerlist) {
$C=Get-QADcomputer -identity $computer -IncludeAllProperties
$strGC=($C.MemberOf).count
$strGRP=($C.MemberOf | foreach {($
.Split(‘,’))[0].Substring(3)}) -Join ","
$strDONE="$C,$strGC,$strGRP"
Add-Content c:\outputfiles\ComputersandGroups.txt -value $strdone
$strDone=$null
$C=$null
$strGC=$null
$strGRP=$null
}


This doesn’t seem to be helping. Furthemore, it seems that even if I click the Stop button to stop the script, the memory used up to that point is not released (If I click Stop at 819MB mem usage for powershell.exe, the 819MB of RAM remains resrved for powershell.exe)

How can I go about getting this information without running out of memory?
by mjolinor at 2013-03-15 11:38:35
This is a good application for pipelining:

Add-PSSnapin Quest.ActiveRoles.ADManagement
$computerlist = Get-Content c:\inputfiles\BIGList.txt
$computerlist |
foreach-object {
$C=Get-QADcomputer -identity $computer -IncludeAllProperties
$strGC=($C.MemberOf).count
$strGRP=($C.MemberOf | foreach {($.Split(','))[0].Substring(3)}) -Join ","
$strDONE="$C,$strGC,$strGRP"
Add-Content c:\outputfiles\ComputersandGroups.txt -value $strdone
}


Foreach-Object will not be as fast as Foreach, but it only needs enough memory for one object at a time.
by notarat at 2013-03-15 12:04:25
Thanks for the response.

I did some additional checking and found a couple other instances where QADComputer and QADUser scripts were using too much memory, so I stopped my running script (it was up to 1.8GB of used memory at that point) and I re-coded the script to use ADcomputer, instead.

Import-Module ActiveDirectory
$computerlist = Get-Content c:\inputfiles\BIGList.txt
foreach($computer in $computerlist) {
$strCMP=GET-ADComputer $computer –Properties cn,MemberOf
$strGRP=($strCMP.MemberOf | Sort Name | foreach {($
.Split(‘,’))[0].Substring(3)}) -Join ","
$strCMP=($strCMP.cn | foreach {($_.Split(‘=’))[0]}) -Join ","
$strDone=$strCMP+","+$strGRP
# Write-Host $strDone }
Add-Content c:\outputfiles\ComputersandGroups.txt -value $strdone
}


While that was running, I came here to post that when I monitored task manager, I never saw the script(now using ADComputer) exceed 51MB.

Before I could complete the post and click "submit" the script completed successfully, lol, so I’ve had to re-write my response. It seems to be working, and not using a lot of RAM, and actually ran pretty fast, to boot.