Create content database

05/06/2014 15:06

param($webAppUrl, $siteUrl)

$script:MyInvocation.MyCommand.Name

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$webApp = Get-SPWebApplication $webAppUrl
$sqlServerName = $webApp.ContentDatabases[0].Server

#Get content database name from siteUrl (last url part)
$sitePart = $siteUrl.Substring($siteUrl.LastIndexOf("/") + 1, ($siteUrl.Length - $siteUrl.LastIndexOf("/")- 1))
if(-not $sitePart)
{
    $sitePart+= "rootSite"
}

$databaseName = "SHP_Content_" + $sitePart

#Create content database if not exists
$newDb = $null
$db = Get-SPContentDatabase ' where {$_.Name -eq $databaseName}
if($db)
{
    write-host ("Content database with name '{0}' already exists." -f $databaseName)
    if($db.Sites -and $db.Sites.Count -gt 0)
    {
        write-host "Following site collections are connected to database." -ForegroundColor Red
        $db.Sites ' % {write-host $_.Url}
    }
    $newDb = $db
}
else
{
    write-host ("Creating content database '{0}'..." -f $databaseName)
    $newDb = New-SPContentDatabase -Name $databaseName -DatabaseServer $sqlServerName -WebApplication $webApp
    $newDb.WarningSiteCount = 0;
    $newDb.MaximumSiteCount = 1;
}
if($newDb)
{
    return $newDb.Name
}
else
{
    return $null
}