Powershell Magik – Testing & Mounting SharePoint 2007 Database to SharePoint 2010


Recently I had a situation where we needed to do a migration testing for our old SharePoint 2007 database, our goal was to Test the Database and Mount them on SharePoint 2010 web application and find out the error, while this was an easy job with executing bunch of Powershell commands, it would have taken more time to do it one by one for each database.

I thought of developing a re-usable PowerShell script to do this for us, It worked really well and I thought it might help some of you.

Here is what the script does for me:-

  1. I write the names of the database in a configuration file (plain text file with one database name each line).
  2. PowerShell script reads the configuration file and iterates through each database.
  3. Executes Test-SPContentDatabase command on the database.
  4. Moves the logs onto appropriate folder.
  5. Executes the Mount-SPContentDatabase command.
  6. Moves all the upgrade log files created today for Mount command.

PowerShell Script:-

Note: Make sure to provide your DBSERVER and WebApplication details.

#Load SharePoint PowerShell Snapin

if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null)

{

Add-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue

}

$DBSERVER = <<DB SERVER NAME>>

#Store the WebApplication URL

$WebApp = “http://webapplication.sp.com

#Read the databases to upgrade

$DB = Get-Content content_db_config.txt

#Loop Through each database and execute Test-SPContentDatabase command

Write-Host “**UPGRADING CONTENT DATABASES**”

foreach ($Data in $DB)

{

write-host “>>TESTING:” $Data

$filename = “C:\UPGRADEDB\logs\” + $Data + “.csv”

Test-SPContentDatabase -Name $Data -WebApplication $WebApp|Export-CSV $filename –noclobber

write-host “>>UPGRADING:” $Data

Mount-SPContentDatabase -Name $Data -WebApplication $WebApp -DatabaseServer $DBSERVER

}

Write-Host “**DATABASE UPGRADE COMPLETED**”

#Copy the upgrade log files onto network Share

Write-Host “**COPY UPGRADE LOG TO NETWORK SHARE**”

$DateToCompare = (Get-date).AddDays(-1)

#Get upgrade logs created today and copy them to network share

foreach ($i in Get-ChildItem “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS\Upgrade*”|where-object{$_.LastWriteTime -gt $DateToCompare})

{

Write-Host “>> Copying” $i.Name

Copy-Item $i.FullName “C:\Upgrade Logs”

}

Write-Host “**LOG FILES COPIED**”

Batch Script:-

In order to make it easily executable, I created a batch script that invokes this powershell script:-

cd C:\UPGRADEDB

powershell -File DBUpgrade.ps1

PAUSE

Hope this helps someone…

Advertisements