SharePoint 2010 Cookbook: PowerShell Script to Automatically Deploy Bamboo Web Parts

Life is too short to wait! Being a Support Engineer at Bamboo Solutions, my everyday work involves installing and testing Web Parts. Basically, I need to install the Web Part to different environments and try to duplicate the same issues that customers are experiencing. Then, when the fix is implemented, I will install it again to check repeatedly until I can confirm that the fix is OK. This obviously takes a lot of time. The Bamboo Solutions Web Part Setup is good because it helps us know what happens through each step. But what if we are already familiar with the process and want to deploy the Web Part quickly? In this post, I will demonstrate how we can easily install a Bamboo Solutions Web Part using PowerShell script and save some time to do more valuable things.

Challenge:

How can I speed up the manual installation of Bamboo Web Parts and automatically deploy Web Parts via a PowerShell script?

Solutions:

Many articles on the Internet explain how to deploy a solution using a PowerShell script. One such article which I found to be particularly useful is from Sohel’s Blog:

http://ranaictiu-technicalblog.blogspot.com/2010/05/sharepoint-2010-deployment-powershell.html

So I’m not going to cover the basics here but instead talk about how we can implement the script to run with the Bamboo Solutions Web Part setup package.

First, verify that you possess the following permissions:

  • SharePoint Farm Administrator.
  • Local Server Administrator on all Web servers.
  • db_owner permission on the content database

In this example, I will deploy Calendar Plus Web Part. After clicking to download the Web Part solution from the Bamboo Solutions store, we are given an .exe file named “HW20.R4.SP2010.SL.exe”. Running it will extract all content to a folder named “HW20.R4.SP2010.SL” and will also open up the Web Part Setup as shown below:

Close the Web Part Setup and take a look at the extracted folder:

We will need to deploy all of the solutions (.wsp) under this folder, except Web License Manager. The logic is simple:

  • Find all .wsp solutions under this folder and filter out the Web License Manager solution.
  • For each solution, check whether it has already been deployed. If yes, remove it.
  • If the solution has not been deployed, install it.

Here is the deployment script (DeployBambooWebpart.ps1):

Add-PsSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.

function WaitForJobToFinish ([string]$solutionName)    # This function waits for the deployment job to finish before doing the next command.
{
    $JobName = "*solution-deployment*$solutionName*"
    $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.."
    }
}

function InstallSolution ([string]$solutionName,[string]$solutionPath)        # This function is used to install a solution.
{
    $solution=Get-SPSolution -Identity $solutionName -ErrorAction:SilentlyContinue    # Get the solution.
    if ($solution -ne $null)        # Check whether the solution exists in the Solutions Store.
    {
    if ($solution.Deployed)        # Check whether the solution is already deployed.
    {
        write-Host $solutionName "is already deployed, going to uninstall it"
        write-Host "Retracting" $solutionName

        if ($solution.ContainsWebApplicationResource)    # If the solution contains Web Application Resource, use this command.
            {
        Uninstall-SPSolution -identity $solutionName -allwebapplication -confirm:$false
            }
            else                        # Otherwise, use this command.
            {
        Uninstall-SPSolution -identity $solutionName -confirm:$false
            }

        Write-Host "Waiting for job to finish"
                 WaitForJobToFinish $solutionName
              }

              Write-Host "Removing" $solutionName            # Remove solution from store.
               Remove-SPSolution –Identity $solutionName -confirm:$false
    }

    Write-Host "Adding" $solutionName "to the Solutions Store"    # Add solution to Solutions Store.
    Add-SPSolution -LiteralPath $solutionPath

    $solution=Get-SPSolution -Identity $solutionName        # Assign solution to a variable.

    Write-Host "Deploying" $solutionName
    if ($solution.ContainsWebApplicationResource)        # If the solution contains Web Application Resource, use this command.
    {
        Install-SPSolution –Identity $solutionName –allwebapplication –GACDeployment -CASPolicies
    }
    else                                # Otherwise, use this command.
    {
        Install-SPSolution –Identity $solutionName –GACDeployment -CASPolicies
    }

    Write-Host "Waiting for job to finish"
    WaitForJobToFinish $solutionName
}

$currentDir=$args[0]    # Assign the script parameter (current directory)

$solutionFiles=Get-ChildItem $currentDir*.* -recurse -include *.wsp -exclude Bamboo.UI.Licensing.wsp    # Get all solutions under this directory and filter out Bamboo Web License Manager.

foreach ($solutionFile in $solutionFiles)            # For each solution found, install it
{
    $solutionName=$solutionFile.Name
    $solutionPath=$solutionFile.FullName

    InstallSolution $solutionName $solutionPath
    Get-SPSolution -Identity $solutionName | select Deployed    # Display deployment result.
    write-Host ""                            # Add an empty line and go on to the next solution
}

Stop-SPAssignment -Global

Remove-PsSnapin Microsoft.SharePoint.PowerShell

write-Host "Finished script."

We can then create a batch file to run this script as follows:

cd /d %~dp0
powershell -noexit -file    ".DeployBambooWebPart.ps1" "%CD%"

Put the script and batch file in the extracted folder (HW20.R4.SP2010.SL) and run the batch file to install this Web Part.

Notes:

  • This method works with both SharePoint 2010 Foundation and Server.
  • This script has not been tested with all Bamboo Solutions Web Parts. Theoretically, it should work with most of them, but it will not work with all of them. For example, if we try to deploy Bamboo List Rollup Web Part R5.0 using this script, we will get an error because some solutions need to be deployed prior to others.

See Also: