Monday, April 16, 2012

Creating multiple site collections in seperate databases and Managed Paths with PowerShell

The Below PowerShell Script to usefull to create Site Collection with diffrent Content Db also each site collection has a managed path (explicit inclusion) and some site collections are using diffrent Web templates.

$ErrorActionPreference = "Stop"

# Creating site collections.
$WebApps = "http://siva:9999" # enter web aplication name Example http://<server>:<port>
$WebApps_url = "http://siva:9999" # enter web aplication URL Example http://<server>:<port>
$db_server = "siva\sharepoint" # Pass the Server name
$owner_alias = "siva\siva.reddy" #Pass the User Name
$sitecoll_templates = @("STS#0","STS#1","STS#2","MPS#1","BLOG#0")
# Foreach loop to create site collections in array.

$SitesToCreate = @("siva1","siva2","siva3","Spblog","Meeting")
$ID = 0
foreach ($site in $SitesToCreate)
    $ID += 1  
    Write-Host "Creating site" $site "( ID =" $ID ")"
    $site_collection_db = "Test_Content_"+$site 
    Write-Host "Site Collectie Database = "$site_collection_db
    # The sitename will be converted to lowercase for the managedpath. 
    $siteToLower = $site.ToLower()
    $SiteColl_URL = $WebApps_url + '/' + $siteToLower
    Write-Host "Site Collectie URL =" $SiteColl_URL
    Write-Host " "
    # Creating content database for each site.
    New-SPContentDatabase $site_collection_db -DatabaseServer $db_server -WebApplication $WebApps
    Write-Host -ForegroundColor Yellow "Content database" $site_collection_db "has been created."
    # Creating managed path for each site collection.
    New-SPManagedPath -RelativeURL $site -WebApplication $WebApps -Explicit
    Write-Host -ForegroundColor Yellow "Managed path" $siteToLower "has been created."
    Switch ($site)
        "Meeting" { $sitecoll_templates = "MPS#1"}
        "Spblog" { $sitecoll_templates = "BLOG#0"}
        default { $sitecoll_templates = "STS#1" }
    # Creating site collection with the name defined by the Switch
    New-SPSite $SiteColl_URL -OwnerAlias $owner_alias -ContentDatabase $site_collection_db -Name $sitename -Template $sitecoll_templates -Language 1033
    Write-Host -ForegroundColor Yellow "Site collection" $site "has been created with the name" $sitename
    # Change status of content database to Disabled so the next site collection can be created in a new Online database.
    Set-SPContentDatabase -Identity $site_collection_db -Status Disabled
    Write-Host -ForegroundColor Yellow "The status of content database" $site_collection_db "is Disabled."

# Change the status of all created content databases to Online.
foreach  ($site in $SitesToCreate)
    $site_collection_db = "SP_Content_"+$site
    Set-SPContentDatabase -Identity $site_collection_db -Status Online
    Write-Host -ForegroundColor Yellow "Status of database" $site_collection_db "is Online."

No comments:

Post a Comment