Monday, February 16, 2015

How to Rename SharePoint 2013 Content Databases with PowerShell

Requirement: To follow standard naming conventions, We got to rename SharePoint content databases in our SharePoint 2013 environment.

Solution:
You can rename a SharePoint content database in these three steps as explained in my another post: How to Rename SharePoint 2013/2010 Central Admin Database and Remove GUID

  1. Detach SharePoint content database from SharePoint web application
  2. Rename the content database in SQL Server
  3. Attach the renamed content database back to SharePoint
rename sharepoint content database powershell

With the help of PowerShell, Lets automate these manual steps.

Rename SharePoint content database with PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Load the assemblies required for the SQL database rename.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")

#Function to Rename Database in SQL Server
function Rename-SQLDatabase($DBServerName, $OldDatabaseName, $NewDatabaseName)
{
    try
    {
        Write-host Establishing connection with SQL Server... -foregroundcolor "Yellow"
        #Connect to the server
        $Server = new-Object Microsoft.SqlServer.Management.Smo.Server($DBServerName)
        
        Write-host Getting the Database in SQL Server... -foregroundcolor "Yellow"
        #Get the database
        $Database = $Server.Databases.Item($OldDatabaseName)

        #Kill all active connections to the SQL database
        $Server.KillAllprocesses($OldDatabaseName)

        Write-host Renaming  Database in SQL Server... -foregroundcolor "Yellow"
        #Rename the database
        $Database.Rename($NewDatabaseName)
        Write-host Database Renamed from $OldDatabaseName to $NewDatabaseName in SQL Server -ForegroundColor Green
    }

    catch
    {
        Write-Error $_.Exception.Message
        Write-Error "Error in Renaming Database in SQL Server!" 
    }
}

#Function to Rename content database in SharePoint
function Rename-ContentDatabase($OldDBName, $NewDBName)
{
    try
    {
        Write-host Getting SharePoint Content Database ... -foregroundcolor "Yellow"
        $ContentDB = Get-SPContentDatabase | Where-object {$_.Name -eq $OldDBName}

        #Get Content Database settings
        $WebApp = $ContentDB.WebApplication.Url
        $DBServer = $ContentDB.Server
        $MaximumSites = $ContentDB.MaximumSiteCount
        $WarningSites = $ContentDB.WarningSiteCount

        #Dismount Content Database
        Write-host Dismounting Content Database ... -foregroundcolor "Yellow"
        Dismount-SPContentDatabase $OldDBName -Confirm:$False

 
        Write-host Renaming Database: $OldDBName to $NewDBName in SQL Server -foregroundcolor "Yellow"

        #Call function to rename Database in SQL Server 
        Rename-SQLDatabase $DBServer $OldDBName $NewDBName

        Write-host Mounting SharePoint content Database... -foregroundcolor "Yellow"
        #Mount the database back
        Mount-SPContentDatabase -Name $NewDBName -WebApplication $WebApp -DatabaseServer $DBServer -MaxSiteCount $MaximumSites -WarningSiteCount $WarningSites | out-null

        Write-host Done!Content Database Renamed from $OldDBName to $NewDBName!! -ForegroundColor Green
    }
     catch
    {

        Write-Error $_.Exception.Message
    }
}

#Call the function to rename database
Rename-ContentDatabase "WSS_Content_310d122490c4303b1c0b3f2f695e7" "HostingFarm_Content_Hosting01" 




You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...