Pages

Search This Blog

Wednesday, April 1, 2015

PowerShell script for removing multiple WSP solutions

In case of big SharePoint Farms with multiple SharePoint WSP solutions deployed, sometimes it is very difficult to remove some specific solutions automatically as each solution retraction takes some time and we may also need to execute SharePoint 2010 Administration service (SPAdminv4) Job after removing individual solution from the Farm. A more manageable way could be to create a PowerShell script, that runs on the Farm to remove the listed wsp solutions. I created one of such solution for a client who had around 50-60 such solutions. Below is the script that you can use to completely uninstall the wsp solutions from SharePoint Farm. This script should be placed inside a folder along with the wsp solutions which needs to be removed. 


Copy-Paste the below script in a text file and rename its extension to - *.ps1

function LoadSharePointPowerShellEnvironment
{
       write-host
       write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
       write-host
       Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
       write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function RetractSolution($packageName)
{
  $spAdminServiceName = "SPAdminV4"
  $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
         if ($solution -ne $null)
         {
                     #Retract the solution
                     if ($solution.Deployed)
                     {
                       Write-Host
                       Write-Host "Retracting solution $packageName..." -foregroundcolor Yellow
                             if ($solution.ContainsWebApplicationResource)
                             {
                                  $solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
                             }
                             else
                             {
                                  $solution | Uninstall-SPSolution -Confirm:$false
                             }
                       Write-Host
                       Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
                       Stop-Service -Name $spAdminServiceName
                       Start-SPAdminJob -Verbose
                       Start-Service -Name $spAdminServiceName

                       #Block until we're sure the solution is no longer deployed.
                       do { Start-Sleep 2 } while ((Get-SPSolution $packageName).Deployed)
                     }
              #Delete the solution
              Write-Host
              Write-Host "Removing solution $packageName..."
              Get-SPSolution $packageName | Remove-SPSolution -Confirm:$false
              Write-Host "Solution Removed Sucessfully." -foregroundcolor Green
         }
         else
         {
               Write-Host
              Write-Host "No solution found to retract with name $packageName" -foregroundcolor Yellow
         }
}





#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                                                    Retract the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#


LoadSharePointPowerShellEnvironment

Write-host "Retracting Solution..." -foregroundcolor Yellow


 $dir = $(gl)
 $list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
 foreach($my_file in Get-ChildItem $list)
 {
       write-host $my_file.Name
       [string]$packageName = $my_file.Name
       RetractSolution $packageName

 }



No comments:

Post a Comment