Export SharePoint solution packages from your environment

Hi,

I created a script that might help someone out there who wants to backup their SharePoint solution packages. This script is able to export specific solutions or all if no solution names are passed to the script.

Sample script calling for specific solutions:

.\script.ps1 –Solutions MYWSPNUM1.wsp,MYWSPNUM2.wsp

Or if you want to export all available solutions then simply call the script without any parameters:

.\script.ps1

# Get Script params, in this case solution names to the -Solution attribute. Separate solutions names by , char
param([String[]] $Solutions)

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

#==================================================================================
# Functions definitions
#==================================================================================

#———————————————————————————-
# This function will export all available solutions in your environment
#———————————————————————————-
function ExportAllSolutions
{
$location = get-location
Write-Host Exporting all available solutions to: $location
foreach ($solution in Get-SPSolution)
{
$id = $Solution.SolutionID
$title = $Solution.Name
$filename = $Solution.SolutionFile.Name

try {
$solution.SolutionFile.SaveAs(“$(get-location)\$filename”)
Write-Host “Exported solution package – ‘$title’:” -foreground green
}
catch
{
Write-Host ” Error with solution package – ‘$title’: $_” -foreground red
}
}
}

#———————————————————————————-
# This Function will export a single solution by solution name
#———————————————————————————-
function ExportSolution
{
param([String] $solutionName)
$location = get-location
Write-Host Exporting solution to: $location
$Solution = Get-SPSolution -Identity $solutionName

$id = $Solution.SolutionID
$title = $Solution.Name
$filename = $Solution.SolutionFile.Name

try {
$solution.SolutionFile.SaveAs(“$(get-location)\$filename”)
Write-Host “Exported solution package – ‘$title’:” -foreground green
}
catch
{
Write-Host ” Error with solution package – ‘$title’: $_” -foreground red
}

}

#==================================================================================
# End of function definitions
#==================================================================================

#==================================================================================
# Main operations for the script
#==================================================================================

# If solutions names are passed then export only those solutions
if($Solutions.count -gt 0)
{
foreach ($solutionName in $Solutions)
{
Write-Host $solutionName
ExportSolution $solutionName
}
}
# If no solultions passed then export all solutions
else
{
ExportAllSolutions
}
#==================================================================================
# End of main operations
#==================================================================================

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s