Mass Deploy and Remove SharePoint 2013 WSP solution packages using PowerShell

Posted: September 30, 2014 in Wep Applications

Whether you are running a large custom-packed-WSP filled SharePoint 2013 Farm or a simple farm with a few different Web Applications, it could be nice to automate the deployment and removal process of WSP-packages to avoid any kind of manual work!

Here’s a way to deploy and remove groups of WSP packages to both -all Web Applications and -spesific Web Applications. The sky is the limit!

Deploy Instructions:
Put all your WSP packages into a folder and define path (1.1).
Specify Web Applications (1.2 ).
Edit the list of WSP package names in groups (1.3 ).
Edit and run deployment function (2.1).

Code:


<#====================================================================
 Copyright © 2014, June. Bjørn Roalkvam
 www.SharePointbjorn.com, bjorn80@gmail.com 
  
 Always test all scripts you find in an test environment prior to production:-), 
 you are a responsible SharePoint Administrator afterall!
====================================================================#>
 
# ===================================================================================
#Checks whether the script is running as admin, if not then starts as admin.
# ===================================================================================
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 
 
"Administrator"))
 
{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
 
#Add sharepoint pssnapin if it doesnt exist:
add-PSSnapin -Name microsoft.sharepoint.powershell -EA "SilentlyContinue"
 
cls
 
 
#---------------------------------------------------------------------------
# 1 Fit to your own solution
#---------------------------------------------------------------------------
 
#Gets the Central Administration URL through code.
$CAURL = Get-SPWebApplication -includecentraladministration | where {$_.IsAdministrationWebApplication} | Select-Object -ExpandProperty Url 
 
#---------
# 1.1 Set the folder path to where the WSP solutions are stored
#---------
$solutionfolder = "D:\Solutions\" 
 
 
#---------
# 1.2 Define Web applications URLs into variables
#---------
$WebApp1URL = Get-SPWebApplication http://portalURL
$WebApp2URL = Get-SPWebApplication http://mysiteURL
$WebApp3URL = Get-SPWebApplication http://hrURL
#You can add more...:
#$WebApp4URL = Get-SPWebApplication http://ICTURL
 
 
#---------
# 1.3 Define WSP Packages to arrays to target spesific Webapplications, 4 are currently
#---------
#Globall - Globally deployed, here you add WSP packages that you want deployed globally.
$GlobalWSP = 
"some.wsp",
"example1.wsp",
"example2.wsp",
"example3.wsp"
 
#$WebApp1URL spesific deploy, here you add WSP packages that you want deployed to this webapplication
$WebApp1WSP =
"example4.wsp",
"example5.wsp",
"example6.wsp"
 
#$WebApp2URL Spesific deply, here you add WSP packages that you want deployed to this webapplication
$WebApp2WSP = 
"example7.wsp", 
"example8.wsp"
 
#$WebApp3URL spesific deploy, here you add WSP packages that you want deployed to this webapplication
$WebApp3WSP =  
"example9.wsp",
"example10.wsp"
 
# You can add more arrays of WSP groups to target any web application...:
#$WebApp4URL spesific deploy, here you add WSP packages that you want deployed to this webapplication ...... 
#$WebApp4WSP =  
#"example11.wsp",
#"example12.wsp"
 
 
 
#---------------------------------------------------------------------------
# The DeployWSP function deployes packages that are not deployed and installs packages that are not installed.
#---------------------------------------------------------------------------
 
Function DeployWSP ($inputWSP, $inputURL, $inputCompabilityLevel)
{
 
    #Check if weburl input
    if($inputUrl) 
    {
        $webapp = get-spwebapplication $inputUrl -EA "SilentlyContinue"
        $inputURL = $webapp.url
    }
 
    #Traverse through solutionlist to check if solutions have been added to farm and deployed.Add and deploy them if not.
    foreach ($solution in $inputWSP)
    {
        $solutiondata = get-spsolution $solution -EA "SilentlyContinue"
 
        #If solution has not been added to farm, then add it.
        if($solutiondata -eq $null) 
        {
            Add-SPSolution ($solutionfolder + $solution)  
            Write-Host "Added solution $solution"
        }
 
        $solutiondata = get-spsolution $solution -EA "SilentlyContinue"
 
        if($solutiondata.Deployed -eq $false)
        {
 
            Write-Host "Deploying $solution"
 
            # Global deploy, no $inputURL parameter has been spesified.
            if(!($webapp))
            {
                Install-SPSolution -Identity $solution -GACDeployment -CompatibilityLevel $inputCompabilityLevel -Force 
            }
 
            # Deploy to Webapplication.
            else
            {
                Install-SPSolution -Identity $solution -GACDeployment -webapplication $inputURL -CompatibilityLevel $inputCompabilityLevel -Force 
            }
 
            # Wait for solution to be deployed, count from 50.
            if($solutiondata)
            {
                $counter = 50
                while( $solutiondata.Deployed -eq $FALSE -and ($counter -gt 0))
                {
                    Write-Host "waiting for installation... $counter"
                    $counter--
                    sleep 3
                }
                $solname = $solutiondata.name
                Write-Host "-----------------------------------"
                Write-Host -f Green "Solution $solname is installed"
                Write-Host "-----------------------------------"
            }
            else
            {
                Write-Host -f Red "Installing $solname has failed. Solution is not found."
            }
        }
    }
}
 
 
#---------------------------------------------------------------------------
# 2 Deploy with function DeployWSP, fit to your solution
#---------------------------------------------------------------------------
#Compabilitylevel targets what sharepoint version you are deploying to, 14 for sharepoint 2010 and 15 for sharepoint 2013. For both set {14,15}
 
 
#---------
# 2.1 Deploy your solutions using the DeployWSP Function
#---------
#You can also deploy single WSP solutions in the -inputWSP parameter. Works both on arrays/arraylists/single packages.
 
DeployWSP -inputWSP $GlobalWSP -inputCompabilityLevel 15
DeployWSP -inputWSP $WebApp1WSP -inputURL $WebApp1URL -inputCompabilityLevel 15
DeployWSP -inputWSP $WebApp2WSP -inputURL $WebApp2URL -inputCompabilityLevel 15
DeployWSP -inputWSP $WebApp3WSP -inputURL $WebApp3URL -inputCompabilityLevel 15
#DeployWSP -inputWSP $WebApp4WSP -inputURL $WebApp4URL -inputCompabilityLevel 15
 
Write-Host ""
Write-Host ""
Write-Host "Completed"
Write-Host ""
Write-Host "Press the anykey to exit"
Read-Host
exit

To Remove WSP solutions you can use:

Code:


#---------------------------------------------------------------------------
# The RemoveWSP function retracts and removes packages
#---------------------------------------------------------------------------

function RemoveWSP ($WSP)
{
	$Solution = Get-SPSolution $WSP -EA "SilentlyContinue"
	if($Solution)
	{	
		Write-Host -f Cyan "Uninstalling $WSP"
		if( $Solution.Deployed -eq $TRUE ) 
		{
			if ($Solution.ContainsWebApplicationResource){ Uninstall-SPSolution $Solution -AllWebApplications -Confirm:0 }			
			if (!( $Solution.ContainsWebApplicationResource)){ Uninstall-SPSolution $Solution -Confirm:0 }
            
			$counter = 50
			while($Solution.JobExists -and ($counter -gt 0))
			{
			    Write-Host -f DarkGray "$WSP is being retracted...time passed:  $counter"
			    $counter--
			    sleep 4
			}
            Write-Host -f Green "$WSP has been successfully retracted"
            Write-Host ""
		}
		Write-Host -f DarkGray "Removing $WSP"
		Remove-SPSolution -Identity $Solution -Force -Confirm:0 
		Write-Host -f Green "$WSP removed from farm"
	}
	if(!($Solution)){Write-Host -f Yellow "Solution $WSP not found, skipping.."}
    Write-Host ""
}


#Remove all Solutions in farm:
Get-SPSolution | % {$_.Name} | % {RemoveWSP $_}


#Remove certain solutions:
RemoveWSP portal.wsp                                                                                                                                                                                                           
RemoveWSP hr.wsp                                                                                                                                                                                                              
RemoveWSP cool.wsp                                                                                                                                                                                                                      
RemoveWSP test.wsp                                                                                                                                                                                                      
RemoveWSP BI.wsp 


#Do a filter on spesific names, and remove these:
Get-SPSolution | ? {$_.name -like "*portal*" -or $_.name -like "*BI*"} | % {RemoveWSP $_}


Advertisements
Comments
  1. b says:

    nahi maja aai ho bhai

  2. Thanks for posting this – this saved me a ton of time

  3. Snehit says:

    Hi,
    I want to do similar thing ,but i want single PS script to check if wsp is present then delete it and install new wsp in single command .

    Thanks.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s