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
With the help of PowerShell, Lets automate these manual steps.
Rename SharePoint content database with PowerShell:
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
- Detach SharePoint content database from SharePoint web application
- Rename the content database in SQL Server
- Attach the renamed content database back to SharePoint
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"