Batch Solution Deploy using PowerShell

Nowadays I’m working on a SharePoint deployment project using Advanced Installer, AutoSPInstaller and custom PowerShell scripts. One of that script deploys a set of solution packages in a given order. Order is important if you have dependencies between the solutions. So I defined  the name of the solutions and the order of them in a xml file. In this post I’d like to share that script and xml.

param 
(
    [string]$WebApplicationUrl = ""
)

Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

Function Pause($action)
{
	Write-Host "Press any key to $action..."
	$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

Function WriteLine
{
	Write-Host -ForegroundColor White "--------------------------------------------------------------"
}

Function WaitForInsallation([string] $Name)
{
	Write-Host -NoNewline "Waiting for deployment job to complete" $Name "."
	$wspSol = get-SpSolution $Name
	while($wspSol.JobExists)
	{
		sleep 2
		Write-Host -NoNewline "."
		$wspSol = get-SpSolution $Name
	}
	Write-Host ""
	Write-Host -ForegroundColor green "Deployment job is finished" 
	WriteLine
}

$Host.UI.RawUI.WindowTitle = " -- Deployment of AIP Solution Packages --"
$0 = $myInvocation.MyCommand.Definition
$env:dp0 = [System.IO.Path]::GetDirectoryName($0)
$bits = Get-Item $env:dp0 | Split-Path -Parent
$wspPath = "$bits\packages"
$configFile = "$wspPath\SolutionDeployConfig.xml"

If (!([string]::IsNullOrEmpty($WebApplicationUrl)))
{
	If (Test-Path -Path $configFile)
	{
		[xml]$xmlinput = (Get-Content $configFile)
		ForEach ($solution in $xmlinput.Solutions.Solution)
		{
			$identity = $solution.getAttribute("Identity")
			$webApplication = $solution.getAttribute("WebApplication")
			Write-Host -ForegroundColor Blue "Deploying solution: $identity"

			$wsp = Get-SPSolution | Where{$_.Name -eq $identity}
			if($wsp -eq $null)
			{
				Write-Host "Adding solution"
				Add-SPSolution -LiteralPath ($wspPath + "\" + $identity)
			}
			else
			{
				Write-Host "Solution already exists"

				if($wsp.Deployed -eq $true)
				{
					Write-Host "solution is deployed already, updating the solution"
					Update-SPSolution -identity $wsp.SolutionId -LiteralPath ($wspPath + "\" + $identity) -GACDeployment
				}
				 else
				{
				   Write-Host "Removing solution"
				   Remove-SPSolution -identity $wsp.SolutionId -confirm:$false

					Write-Host "Adding solution"
				   Add-SPSolution -LiteralPath ($wspPath + "\" + $identity)
				}
				WaitForInsallation -Name $wsp.Name
			}

			$wsp = Get-SPSolution | Where {$_.Name -eq $identity}
			if($wsp -ne $null)
			{
				Write-Host "Installing solution"

				if ($webApplication)
				{
					Install-SPSolution -identity $wsp.SolutionId -WebApplication $WebApplicationUrl -GACDeployment -force
				}
				Else
				{
					Install-SPSolution -identity $wsp.SolutionId -GACDeployment -force
				}
			}
			WaitForInsallation -Name $wsp.Name

		}

		WriteLine
		Write-Host -ForegroundColor Blue "Deployment is completed"
		WriteLine
		Pause "exit"
	}
	Else
	{
		Throw " - Cannot locate solution deployment configuration file; please check that the SolutionDeployConfig.xml file is in the \packages subfolder."
	}
}
Else
{
	Throw " - Please provide the web application url parameter -WebApplicationUrl"
}

And the following xml is the configuration file of the deployment. It contains the packages to be deployed. Script will deploy them in the order of definition. I used WebApplication atrribute to specify if that solution will be deployed to a specific web application (url is set by powershell script parameter) or not.

<?xml version="1.0" ?>
<Solutions>
	<Solution Identity="logging.wsp" />
	<Solution Identity="webconfigmodifications.wsp" />
	<Solution Identity="configuration.wsp" />
	<Solution Identity="copysitecollection.wsp" />
	<Solution Identity="branding.wsp" />
	<Solution Identity="webparts.wsp" WebApplication="true" />
	<Solution Identity="search.wsp" WebApplication="true" />
</Solutions>

Hope this helps in your deployment tasks…

Leave a comment

Your email address will not be published.

3 thoughts on “Batch Solution Deploy using PowerShell”

%d bloggers like this: