Wednesday, June 26, 2013

Database Movement from One Web Application to another using Powershell

We may have necessity of moving databases of one web application the other.
If the databases are in bulk, then we move ahead by automating with power shell as below
Below is the script that has been written to move database from one web application the other
// DBMove.bat
cd /d %~dp0
@echo off
powershell.exe -noexit .\DBMove.ps1 .\DBMove.csv
// DBMove.csv
SourceWebAppUrl,DestinationWebAppurl
// DBMove.ps1
#-----Input parameters to the script
param($inputFile)

#------To check if Sharepoint cmdlets are registered are registered
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null )
{
            Add-PSSnapin Microsoft.SharePoint.Powershell 
}
#---------------------------------------------------------------------------------------------------
#-----------------------------------------Start Logging---------------------------------------------
$filepath = $MyInvocation.MyCommand.Definition                                        
$directorypath = [System.IO.Path]::GetDirectoryName($filepath)
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\DBMove_Log_$LogTime.txt"
Start-Transcript -Path $LogFile -Force
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
function DBMove()
{
param([string] $swebAppUrl,[string] $dwebAppUrl)

$SourceWebApp = Get-SPWebApplication -Identity $sourcewebAppUrl
$DestinationWebApp = Get-SPWebApplication -Identity $destinationwebAppUrl

if($SourceWebApp -ne $null -and  $DestinationWebApp -ne $null)
{

$SourceDBlists=Get-SPContentDatabase -WebApplication $SourceWebApp
Get-SPContentDatabase -WebApplication $SourceWebApp  | Dismount-SPContentDatabase -confirm:$false
foreach ($SourceDBlist in $SourceDBlists)
{
 Mount-SPContentDatabase $SourceDBlist.Name -DatabaseServer $SourceDBlist.Server -WebApplication $destinationwebAppUrl
}
}
else
{
  Write-Host 'Not a valid Web application url...'
}

}

#---------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------
#loop through a csv file
Import-Csv $inputFile | ForEach{                            
    $SourcewebAppUrl = $_. SourceWebAppUrl
    $destinationAppUrl = $_.DestinationWebAppurl
              
    try
    {
  write-host "Moving Source Database from $sourcewebAppUrl to $destinationwebAppUrl...."
        SourceDBMove $sourcewebAppUrl,$destinationwebAppUrl
  write-host "Completed..."
    }
    catch
    {
  write-host "Completed with errors..."   
        Write-Output $_
    }
}
#---------------------------------------------------------------------------------------------------

#-----------------------------------------End Logging-----------------------------------------------
Stop-Transcript
#---------------------------------------------------------------------------------------------------


No comments:

Post a Comment