Archive for June 27, 2014

Create Site Collection with Content Database

A PowerShell related post today.

If you need to create a new site collection and ensure it uses a specific content database, this little script will get that done for you. No messing about in Central Administration disabling content databases and all that good stuff, just a simple script that gets the job done, hassle free.

$hostIP = $ENV:ComputerName
$hostURL = "http://$hostIP/sites/Site Collection Name"
$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$dbServer = "$hostIP"
$contentDbName = "Your_Content_Db_Name"
$webAppName = "SharePoint - 80"
$lcid = 1033
$templateName = "STS#0"
$siteName = "Site Collection Name"
 
function Add-Site {
	Write-Host ("Creating new ComplianceSP site '", $siteName, "' at ", $hostURL -join "") -NoNewline
	$newSite = New-SPSite $hostURL -OwnerAlias $user -name $siteName -Template $templateName -Language $lcid -ContentDatabase $contentDbName
	$newSite.RootWeb.CreateDefaultAssociatedGroups($newSite.Owner, $null, $null)
	Write-Host " - Done." -ForegroundColor Green
}
 
function Add-ContentDatabase {
	$contentDatabase = Get-SPContentDatabase -WebApplication $webAppName | Where { $_.Name -eq $contentDbName }
	if ($contentDatabase -eq $null) {
		Write-Host ("Creating new content database '", $contentDbName, "' in '", $dbServer, "' for web application '", $webAppName, "'" -join "") -NoNewline
		New-SPContentDatabase -Name $contentDbName -DatabaseServer $dbServer -WebApplication $webAppName -MaxSiteCount 1 -WarningSiteCount 0 -Confirm:$false
		Write-Host " - Done." -ForegroundColor Green
	}
	else {
		Write-Host ("Content database '", $contentDbName, "' in '", $dbServer, "' associated with web application '", $webAppName, "' already exists." -join "") -NoNewline
	}
}
 
try {
	Add-ContentDatabase
	Add-Site
}
catch {
	Write-Host ""
	Write-Host "Error : " $Error[0] -ForegroundColor Red
	throw
	Exit 1
}

Lets walk through the code.
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: