当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 重命名 SharePoint 2013 内容数据库?

作者:精品下载站 日期:2024-12-14 21:01:37 浏览:13 分类:玩电脑

如何使用 PowerShell 重命名 SharePoint 2013 内容数据库?


要求:为了遵循标准命名约定,我们必须在 SharePoint 2013 环境中重命名 SharePoint 内容数据库。

解决方案:

您可以通过以下三个步骤重命名 SharePoint 内容数据库,如我的另一篇文章中所述:如何重命名 SharePoint 2013/2010 中央管理数据库并删除 GUID?

  1. 将 SharePoint 内容数据库与 SharePoint Web 应用程序分离
  2. 在 SQL Server 中重命名内容数据库
  3. 将重命名的内容数据库附加回 SharePoint

[玩转系统] 如何使用 PowerShell 重命名 SharePoint 2013 内容数据库?

在 PowerShell 的帮助下,让我们自动化这些手动步骤。

使用 PowerShell 重命名 SharePoint 内容数据库:


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" 

替代方法:使用所需名称创建内容数据库,使用 cmdlet Move-SPSite 将所有网站集从现有数据库移动到新数据库,然后卸载旧内容数据库。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯