Detach / Attach a Site Collection's Content Database

This code will detach and then reattach a site collection's content database.
I use this, along with the SQL/SMO code, to perform regular database restorations in DEV or TEST environments, from PROD backup files.

Note: the content database may contain other site collections as well
Language:
PowerShell
Keywords:
SharePoint
Code Snippet

# Variables
$tSite_Url = "http://spserver/sitecollection"

# Load the SharePoint assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# Find the Content Database
$tSite = New-Object Microsoft.SharePoint.SPSite(
$tSite_Url)
$tWebApp = $tSite.WebApplication
$tContentDB = $tSite.ContentDatabase
$tSite.Dispose()

# Remove/Detach the Content Database
$tWebApp.ContentDatabases.Delete($tContentDB.Id)

 

#####################################################


# Add/Attach the content database
$tWebApp.ContentDatabases.Add($tContentDB_Server, $tContentDB_Name, $null, $null, 9000, 15000, 0)


Created 2012-05-07
comments powered by Disqus
Login