PowerShell || Retract and remove sharepoint solution

1

November 6, 2013 by Ravi C Khambhati

So with this post I am not sharing somthing extraordinory but this will help your life a lot easier while you are rebuilding your environment quite frequently. Also this scrip does not have too much functionaly so let me know your suggetions/commets and will share improved script.

So as mentioned in the subject line this powershell script will retract and remove solution on top of that script will also take xml file as argument. This file will have list of solutions and scrip will go one by one.

Below are the steps which will be performed by script,

  • Check for input parameter if not passed then error will be shown and script will not run
  • Iterate through all solution
  • Check whether solution is exist or not if not then skip the solution with error message
  • If solution is exist then Uninstall the solution and wait till operation is completed.
  • Once solution is uninstalled then remove the solution

PowerShell Script

param([String]$solutionListFileName)

if ( $solutionListFileName -eq '' )
{
	Write-Host 'Solution list file parameter missing' -ForegroundColor Red
	return
}

[xml]$root = Get-Content $solutionListFileName
foreach( $solution in $root.Solutions.Solution)
{
	$solutionName = $solution.Name

	#check whether solution is exists or not
	$searchResult = Get-SPSolution | where {$_.Name -eq $solutionName }
	if ( $searchResult -eq $null )
	{
		Write-Host "Solution is not exist:$solutionName" -ForegroundColor Red
		#if solution is not exist continue to next solution
		continue;
	}

	Write-Host "Uninstaling solution $solutionName" -ForegroundColor Green

	$isWebApplication = [System.Convert]::ToBoolean($solution.IsWebApplication)

	#if solution contains resources scoped for a web application
	if ( $isWebApplication -eq $true)
	{
		Uninstall-SPSolution -Identity "$solutionName" -AllWebApplications -Confirm:$false
	}
	#if solution does not contains resources scoped for a web application
	else
	{
		Uninstall-SPSolution -Identity "$solutionName" -Confirm:$false
	}

	#waiting for uninstall to complete
	$wspSolution = Get-SPSolution -Identity:$solutionName
	while ($wspSolution.JobExists -eq $true) {
	    Write-Host '.' -NoNewline
	    sleep -Seconds:1
	    $wspSolution = Get-SPSolution -Identity:$file
	}
	Write-Host
	Write-Host "$solutionName has been uninstalled" -ForegroundColor Green

	Write-Host "Removing solution $solutionName" -ForegroundColor Green
	Remove-SPSolution -Identity "$solutionName" -Confirm:$false
	Write-Host "$solutionName has been removed" -ForegroundColor Green
}

XML input file format

<?xml version="1.0" encoding="utf-8"?>
<Solutions>
  <Solution>
    <Name>solution1.wsp</Name>
    <IsWebApplication>false</IsWebApplication>
  </Solution>
  <Solution>
    <Name>solution2.wsp</Name>
    <IsWebApplication>true</IsWebApplication>
  </Solution>
</Solutions>

I am open to any suggestions so please share your ideas and will update the script as per your suggestions.

Google+

Advertisements

One thought on “PowerShell || Retract and remove sharepoint solution

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

%d bloggers like this: