Export/Import SharePoint 2010 solutions using PowerShell (Part 2/3)


Following up on the exporting of all solutions deployed in SharePoint 2010 in part 1, now it’s time to import these solutions to a different SharePoint 2010 farm.

Part 2 – Import all exported solutions to any SharePoint 2010 environment

In this part, we will cover how to add the solutions to another SharePoint 2010 farm. This is equal to using the “Add-SPSolution” command in PowerShell, only it does this for all solutions in a given directory.

Here is the script we will use:

#This script will add all solutions in the selected folder
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Functions
function getPath
{
    #Get the directory that contains the solutions
    $inputPath = Read-Host "Enter the full path that contains the solutions (eg. C:\solutions)"
    return $inputPath
}

function addSolutions($importPath)
{
$counterItemsAdded = 0
Write-Host "Adding the solutions to the SharePoint environment" -fore green

#Check if specified path exists, if it does, add all solutions in the directory
if((Test-Path $importPath) -eq $True)
    {
    $items = Get-ChildItem $importPath | ?{$_.extension -eq ".cab" -or $_.extension -eq ".wsp"} #Gets all files that have the .cab or .wsp extension
   #The following line checks all currently installed solutions and adds these to $solutions
    $solutions = (Get-SPFarm).Solutions | Foreach-Object{$solnames += $_.name;} #Get all current SPSolutions
   
        foreach($item in $items)
        {
        #If the solution does not exist within the current environment
        if($solnames -notmatch $item.toString())
            {
                Add-SPSolution -LiteralPath $item.Fullname | out-null
                Write-Host "Imported $item" -fore green
                $counterItemsAdded += 1
            }
        else
            {       
                Write-Host "$item already exists on the SharePoint environment." -fore red
            }
        }
        #If at least 1 solution has been added
        if($counterItemsAdded -ge 1)
        {
        Write-Host "The solutions have been added`n" -fore green
        Write-Host "Remember, the solutions will not be available until you deploy them" -fore yellow
        }
        else
        {
        Write-Host "The specified directory didn’t contain any valid .WSP or .CAB files" -fore red
        }
    }
#If the current path does not exist, ask for the path again   
else   
    {
    Write-Host "Directory does not exist. `nPlease enter the path in the following format: C:\Solutions" -fore yellow
    addSolutions(getPath)
    }
}

Write-Host "This script will add all solutions in the specified directory to SharePoint 2010" -fore green

#Call functions
addSolutions(getPath)

The output of this script (when there are actually some valid .wsp files in the directory) will look something like this:

image

Like the output says, in order to use the solutions, you will have to deploy them first. You can do this manually, by using the PowerShell command “Install-SPSolution”, or you can go to part 3, where I created a script to do deploy ALL added solutions to your web applications.

Leave a comment