Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

PowerShell script to populate farm kills memory

  Date: May 29    Category: Sharepoint    Views: 1109

I have a powershell script that I wrote to populate a SharePoint farm. (This is
not for production, this is for testing of 3rd party products.)

Unfortunately, the script I have manages to choke the memory. I would like to
know how I can release the used memory after each pass through each loop.

The script is copied inline below. It works great until it gets to about 100
Site Collections created and then falls over and dies with a "System Out of
Memory Exception." I know what the issue is, just not quite sure how to get the
script to let go of the memory.

(The idea of the script is to automate the construction of a large information
architecture. 4 variables can be adjusted to increase or decrease the number of
objects at each level as needed.)

Start-SPAssignment -Global
try {
while ($a -lt 2)

$WebApplication = "WebApp"+$a.tostring()
$AppPool = "AppPool"+$a.tostring()
Write-Host "Creating Web Application "$WebApplication $Port
New-SPWebApplication -ApplicationPool $AppPool -Name $WebApplication
-ApplicationPoolAccount "domain\user" -Port $port
while ($b -lt 2)

$DB = "_Content"+$b.tostring()
$ContentDB = $WebApplication+$DB
Write-Host "Creating Content DB " $ContentDB "on" $WebApplication
New-SPContentDatabase $ContentDB -WebApplication $WebApplication
while ($c -lt 301)


Write-Host "Creating Site Collection at "$SiteCollection
New-SPSite -Url $SiteCollection -OwnerAlias "domain\user"
-Description "Script Generated Site" -Name $Site -Template "STS#0"
while ($d -lt 3)

Write-Host "Creating Sub site "$SubSite "in "$SiteCollection
New-SPWeb -Url $SubSite -Template STS#0 -Name $SubSite
-AddToQuickLaunch -AddToTopNav




catch [System.SystemException]
write-host "The script has stopped because there has been an error.
Stop-spassignment -global



1 Answer Found

Answer #1    Answered By: Thisis Myrealname     Answered On: Jul 04

1. You need to call dispose on your $SiteCollection objects. The problem here is you're creating hundreds of massive objects (sites and site collections) in memory and they're never leaving scope. This has the same effect as tail recursion. Your scope in this case is the script, as your try/catch wraps all functionality.

2. Powershell will also clean up functionality if you don't maintain a reference, so wrapping the create statements in a function would work as well, albeit not as obviously.

3.You're also never using $SubSite more than once at a time, so that should be declared as a script level variable.

4. Give your variables meaningful names and stop with the lazy vb code:
$a should be $webAppCount
$b should be flat go away, it's the same as $a
$c should be $siteCollectionCount
$d should be $siteCount

5. Your nested while loops are making your code ugly and unmanageable:
for($webAppCount = 0; $a -lt 1; $webAppCount++)
#do stuff

6. Don't use looping constructs a single instance. If you want to specify an iteration limit, look up powershell params

7. 4 and 5 are my opinions My opinions are also correct.

Didn't find what you were looking for? Find more on PowerShell script to populate farm kills memory Or get search suggestion and latest updates.