Tuesday 18 October 2011

Restore a site collection SharePoint Server 2010 using PowerShell

I have just done some migrations from SharePoint 2010 test to production environments using PowerShell.

Some information on how easy this is can be found here.
Restore a site collection (SharePoint Server 2010)

Should you use PowerShell over stsadm.exe?

If you are anything like me you have already done hundreds of such jobs with stsadm.exe. Stsadm.exe is still supported in SharePoint 2010 so why should you take the time to learn a new scripting language?

Well the answer is simple, PowerShell is just so much better than stsadm.exe.  PowerShell should be used for the following reasons:


  1. It is now Microsoft Recommendation, don't want to go against that.
  2. The language is much more elegant, easier to read and write once you get a hang of it.
  3. The language now supports functions and variables, it is now more of a programming language.
  4. Its more powerful, allowing you do things like putting site collections in new databases.
So is PowerShell just the new version of stsadm.exe?

No.

PowerShell provides a scripting language very much like PHP.  In fact it is so like PHP that anyone who has done any PHP code will have no problem picking it up. Even if you don't know PHP anyone with basic programming skills will be able to pick PowerShell up easily. For example the following 'Hello world' command will look familiar to any programmer. 


function HelloWorld()
{ 
    $say = "Hello world"


    echo $say
}



This ability to use functions and variables allows you to write, and pre-test powerful PowerShell solutions.  For example imagine trying to script something like this in stsadm.exe


function WaitForJobToFinish([string]$SolutionFileName)
{ 
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job.."
    }
}

Add-PsSnapin Microsoft.SharePoint.PowerShell
 
$CurrentDir=$args[0]
$solutionName="Limeco.UI.WebParts.wsp"
$SolutionPath=$CurrentDir + "\"+$solutionName 
 
Write-Host 'Going to disable feature'
disable-spfeature -identity Limeco.UI.WebParts_LimecoWebPartFeature -confirm:$false -url http://localhost
 
Write-Host 'Going to uninstall feature'
uninstall-spfeature -identity Limeco.UI.WebParts_LimecoWebPartFeature -confirm:$false -force
 
Write-Host 'Going to uninstall solution'
Uninstall-SPSolution -identity $solutionName  -allwebapplications -confirm:$false

Write-Host 'Waiting for job to finish'
WaitForJobToFinish 

Write-Host 'Going to remove solution'
Remove-SPSolution –entity $solutionName -confirm:$false
 
Write-Host 'Going to add solution'
Add-SPSolution $SolutionPath
 
Write-Host 'Going to install solution to all web applications'
Install-SPSolution –entity $solutionName –llwebapplications –ACDeployment

Write-Host 'Waiting for job to finish' 
WaitForJobToFinish 

Write-Host 'Going to enable Feature' 
Enable-spfeature -identity Limeco.UI.WebParts_LimecoWebPartFeature -confirm:$false -url http://localhost 

Remove-PsSnapin Microsoft.SharePoint.PowerShell

Source Sohel Rana

Hey, I am a System Admin not a Programmer, isn't PowerShell just over the top

No again.

PowerShell basic functions are much cleaner and easier to run than the same stsadm.exe code.

Example of PowerShell for dummies

Here is a use case.  You have been given two backup files from a development box and you need to install the two sites in a web application, each in its own site collection with its own database.  We assume here you have already added a second content database.

The web application is on port 80, and is called http://test  and the two content databases are WSS_Contet and WSS_Content2.  You need to update 2 backup files, one at D:\backups\home.bak and D:\backups\work.bak.  Work.bak will be restored to a site collection at http:/sites/work

You are just going to be running PowerShell, you don't have time to make any saved scripts.  You were thinking about just using stsadm but your boss, say a project manager, says you must use PowerShell.

Well the good news it is easy.  Very easy to do this.

We assume you know how to get in to PowerShell here, if you don't here is a helpful introduction.

So you are logged in to PowerShell with the appropriate rights to the database.  I have generally logged in to the server with the Farm account and not the Admin accounts

The first step I do is to list out the Web Applications on the server.  You type the PowerShell command:

Get-SPWebAppllication

And you get a list of web applications, there ports and their URLs.  One of the URLS in the example is http://test

Now to restore  the back to http://test on WSS_Content type the following PowerShell command:

Restore-SPSite http://test -path D:\backups\home.bak -DatabaseName WSS_Content

PowerShell will ask you to confirm that you rally want to do this.  Wait until the task completes.

Then to build the second site type:

Restore-SPSite http://test/sites/work -path D:\backups\work.bak -DatabaseName WSS_Content_2

This then takes care of building the new site collection for you, and puts it in the database WSS_Content_2.

Its that easy!


No comments:

Post a Comment