Monday, May 15, 2017

PowerShell Script : Mount Multiple Content Databases at once with SharePoint

Below is the code that you can use to mount multiple content databases with SharePoint (2010 or 2013 or 2016). Put all of the content databases names in DB.txt file and run the script.

1. Create text file with name “DB.txt” in the same directory where script file is present.
Write database names in “DB” file. 
Make it sure each name should be in new line and write End Point at the end as shown below.


2. Open SharePoint 2013 Management Shell


3. Change directory path to path where script file is saved.


Then type ./MountSPContentDB.ps1 and press enter key. (Copy the script in a file and name it MountSPContentDB.ps1) 
Provide web application url and press enter key.


Provide SqlServer name and instance. 

4. Result 
You can track the complete log file (named MountSPContentDB -current date and time) which you can find in the same directory where your script file is located. 

Script:
[code]$currentTime = Get-Date -Format o | foreach {$_ -replace ":", "."} New-Item $PSScriptRoot\UpgradeSPContentDB-$currentTime.txt -type file $webAppUrl = Read-Host "Please provide Web Application URL" $databaseServer = Read-Host "Please provide database server name" $DbArray = Get-Content -Path $PSScriptRoot\DB.txt function UpgradeSPContentDB() { Start-Transcript -Path "$PSScriptRoot\UpgradeSPContentDB-$currentTime.txt" foreach ($dbName in $DbArray) { if($dbName -ne "End Point") { try{ Mount-SPContentDatabase $dbName -DatabaseServer $databaseServer -WebApplication $webAppUrl -ErrorAction Stop Write-Output "$dbName is upgraded" } catch [System.Exception] { Write-Host $_.Exception.Message } } } Stop-Transcript } UpgradeSPContentDB[/code]

No comments:

Post a Comment