4

Is there a easy way to divide site collection equally between the number of content databases attached to a single web application?

I'm having a single content databases where a lot of sites were added rapidly and have now added multiple other content databases so that all the new sites being created are distributed equally between the 10 content databases.

Now i would like to distribute the site collections from my default/first content database equally to the databases available without having to move sites by setting the number of sites to be moved manually using the -limit parameter available to the Get-SpSite cmdlt.

Wrote some quick PowerShell but there must be an easier way to do this?

SharePoint 2013.

Add-PSSnapin microsoft.sharepoint.powershell

#Get the web application where the site collections should be distributed equally between the current amount of content databases
$webApp = Get-SPWebApplication -Identity https://Webapplication.domain.com

#Count the number of content databases attached to the web application
$noDbs = $webApp.ContentDatabases.Count

#Get all the content databases attached to the web application
$db = Get-SPContentDatabase -WebApplication $webApp

#Count the total number of sites in all of the content databases attached to the web application
$noSites = $db.Sites.Count

#Divide the total number of site collections in the webapplication divided with the number of total content databases attached to the webapplication
$divSitesDbs = $noSites / $noDbs

#Round the result to the nearest integer
$roundSites = [math]::Round($divSitesDbs)

#Get a batch of sites from the calculation above, and move/distribute them equally between the number of content databases attached to the web application
foreach($contentDB in $db) {
    Get-SPSite -Limit $roundSites -WebApplication $webApp | Move-SPSite -DestinationDatabase $db.name
    }
  • In general this isn't the best strategy to take. Not all site collections are equal, e.g. I could have an SC that is 5GB and another that is 300GB. Do they belong in the same CDB? Probably not. Consider site size and site growth as part of your database planning, not just site count. – Trevor Seward Jul 7 '17 at 14:42
  • @TrevorSeward, Thanks for taking your time Trevor. My problem is that none of the sites has had the chance to grow bigger yet as we are still setting sites up, and we do not really know today which of them that will grow bigger but we are having schedules to monitor the sizes and will be adding new CDB in the future. – Christoffer Jul 10 '17 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.