Wednesday, June 15, 2011

How to create site collection with dedicated content database through script

Quite often, for performance and scalability reason, we'd want to put new site collection into a dedicated content database. Below is the PowerShell script I built in 2 hours.  It's not perfect, but good enough in most of the cases.

Publishing feature is enabled by default.

$template = Get-SPWebTemplate "STS#0"
$webapp = "http://SharePointServer"
$owner1 = "domain\spadmin1"
$owner2 = "domain\spadmin2"

function CreateSiteCollection([string]$sitename)
{
$dbname = ("WSS_Content_80_" + $sitename)
$siteurl = ($webapp + "/sites/" + $sitename)

Write-Host "dbname=" $dbname ", siteurl=" $siteurl -ForegroundColor Yellow

new-spcontentdatabase -name $dbname -webapplication $webapp
new-spsite -name $sitename -ContentDatabase $dbname -url $siteurl -OwnerAlias $owner1 –SecondaryOwnerAlias $owner2 -Template $template
Get-SPContentDatabase -Site $siteurl | Set-SPContentDatabase -MaxSiteCount 1 -WarningSiteCount 0
Enable-SPFeature -Identity "PublishingSite" -Url $siteurl
$site = Get-SPSite $siteurl
$site | Get-SPWeb -limit all | ForEach-Object {Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url}

$web = $site.RootWeb


$groupname = ($sitename + " Owners")
$newGroup = $web.SiteGroups.Add($groupname, $web.CurrentUser, $null, "Use this group to grant people full control permissions to the SharePoint site")
$web.AssociatedOwnerGroup = $newGroup
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$groupname])
$roleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions["Full Control"])
$web.RoleAssignments.Add($roleAssignment)

$groupname = ($sitename + " Members")
$newGroup = $web.SiteGroups.Add($groupname, $web.CurrentUser, $null, "Use this group to grant people contribute permissions to the SharePoint site")
$web.AssociatedMemberGroup = $newGroup
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$groupname])
$roleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions["Contribute"])
$web.RoleAssignments.Add($roleAssignment)

$groupname = ($sitename + " Visitors")
$newGroup = $web.SiteGroups.Add($groupname, $web.CurrentUser, $null, "Use this group to grant people read permissions to the SharePoint site")
$web.AssociatedVisitorGroup = $newGroup
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($web.SiteGroups[$groupname])
$roleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions["Read"])
$web.RoleAssignments.Add($roleAssignment)

$web.Update()
$web.Dispose()


$site.Dispose()
}

CreateSiteCollection("site1")
CreateSiteCollection("site2")

rm function:/CreateSiteCollection

No comments:

Post a Comment