{"id":1682,"date":"2014-06-27T17:37:13","date_gmt":"2014-06-27T16:37:13","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1682"},"modified":"2014-06-27T17:37:13","modified_gmt":"2014-06-27T16:37:13","slug":"create-site-collection-with-content-database","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2014\/06\/27\/create-site-collection-with-content-database\/","title":{"rendered":"Create Site Collection with Content Database"},"content":{"rendered":"<p>A PowerShell related post today.<\/p>\n<p>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.<\/p>\n<pre lang=\"PowerShell\">\r\n$hostIP = $ENV:ComputerName\r\n$hostURL = \"http:\/\/$hostIP\/sites\/Site Collection Name\"\r\n$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name\r\n$dbServer = \"$hostIP\"\r\n$contentDbName = \"Your_Content_Db_Name\"\r\n$webAppName = \"SharePoint - 80\"\r\n$lcid = 1033\r\n$templateName = \"STS#0\"\r\n$siteName = \"Site Collection Name\"\r\n\r\nfunction Add-Site {\r\n\tWrite-Host (\"Creating new ComplianceSP site '\", $siteName, \"' at \", $hostURL -join \"\") -NoNewline\r\n\t$newSite = New-SPSite $hostURL -OwnerAlias $user -name $siteName -Template $templateName -Language $lcid -ContentDatabase $contentDbName\r\n\t$newSite.RootWeb.CreateDefaultAssociatedGroups($newSite.Owner, $null, $null)\r\n\tWrite-Host \" - Done.\" -ForegroundColor Green\r\n}\r\n\r\nfunction Add-ContentDatabase {\r\n\t$contentDatabase = Get-SPContentDatabase -WebApplication $webAppName | Where { $_.Name -eq $contentDbName }\r\n\tif ($contentDatabase -eq $null) {\r\n\t\tWrite-Host (\"Creating new content database '\", $contentDbName, \"' in '\", $dbServer, \"' for web application '\", $webAppName, \"'\" -join \"\") -NoNewline\r\n\t\tNew-SPContentDatabase -Name $contentDbName -DatabaseServer $dbServer -WebApplication $webAppName -MaxSiteCount 1 -WarningSiteCount 0 -Confirm:$false\r\n\t\tWrite-Host \" - Done.\" -ForegroundColor Green\r\n\t}\r\n\telse {\r\n\t\tWrite-Host (\"Content database '\", $contentDbName, \"' in '\", $dbServer, \"' associated with web application '\", $webAppName, \"' already exists.\" -join \"\") -NoNewline\r\n\t}\r\n}\r\n\r\ntry {\r\n\tAdd-ContentDatabase\r\n\tAdd-Site\r\n}\r\ncatch {\r\n\tWrite-Host \"\"\r\n\tWrite-Host \"Error : \" $Error[0] -ForegroundColor Red\r\n\tthrow\r\n\tExit 1\r\n}\r\n<\/pre>\n<p>Lets walk through the code.<br \/>\n<!--more--><br \/>\nFirst off we declare a number of variables that are used to create the site collection and associate it with a specific content database.<\/p>\n<p>$hostURL stores the fully qualified URL of the site collection you want to create.<br \/>\n$dbServer references the database server, as you probably guessed \ud83d\ude42<br \/>\n$contentDbName should contain the name of the content database you want to associate with the site collection<br \/>\n$webAppName is the name of the web application the site collection will belong to.<br \/>\n$lcid is the locale id to be used when creating the site collection.<br \/>\n$templateName is the name of the SharePoint template to use for the root of the new site collection.<br \/>\n$siteName contains the name of the root site.<\/p>\n<p>The first section of code that is called is to Add-ContentDatabase. What this method does is attempt to retrieve the content database specified by $contentDbName within the configured web application. If the content database exists the method exits without doing anything else.  If it does not exist, it then calls New-SPContentDatabase and creates the content database with a maximum site count of 1, this will allow it to only be used by the site collection that&#8217;s created next.<\/p>\n<p>The final piece of the script is a call to the Add-Site method.<\/p>\n<p>Here, we call New-SPSite and importantly set the content database parameter to $contentDbName.  Once this has completed we ensure the new site&#8217;s owner is a member of the site owners group.<\/p>\n<p>Nothing too complicated by handy to know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2014\/06\/27\/create-site-collection-with-content-database\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3],"tags":[41,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-r8","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1682"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1682"}],"version-history":[{"count":19,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1682\/revisions"}],"predecessor-version":[{"id":1705,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1682\/revisions\/1705"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}