Backup SharePoint solution before deploying its new version using Release Management

Just wanted to share the PowerShell script to backup SharePoint solutions using Release Management. This script can be used as a part of a custom tool/action in Release Management that will backup existing SharePoint solution before deploying a new version of this solution. This tool/action could come in handy when you would like to set up rollback activities for when SharePoint solution install goes sideways. I think it's a good idea to back up things. You know, just in case…

Here is a script. It's not the most sophisticated PowerShell script, but it gets the job done. It takes two parameters: name of the SharePoint solution and the destination folder where you would like to store the backed up solution file.

param

(

[string]$WSP_FileName = $null,

[string]$DestinationName = $null

)

# Load SharePoint snap-in

$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }

if ($snapin -eq $null) {

Write-Host "[INIT] Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"

}

 

Write-Host "[INFO] ----------------------------------------"

Write-Host "[INFO] Backing up $WSP_FileName"

$farm = Get-SPFarm

$file = $farm.Solutions.Item("$WSP_FileName").SolutionFile

$BackupPath = Join-Path $DestinationName $WSP_FileName

if ($file.Count -ne 0)

    {

$file.SaveAs("$BackupPath")

Write-Host -f Green "...Backup complete!"

    }

else

{

    throw "$WSP_FileName solutiuon was not found."

}

 

##################################################################################

# Indicate the resulting exit code to the calling process.

if ($exitCode -gt 0)

{

"`nERROR: Operation failed with error code $exitCode."

}

"`nDone."

exit $exitCode

 

Here is a link to download the script.