Deploy SharePoint solution in Release Management

Here is another PowerShell script that can help you deploy SharePoint solutions using Release Management. This PowerShell script can be used as a part of a custom tool/action/component in Release Management that will deploy (or first retract, then deploys) a SharePoint solution. It's fairly straightforward script that might not necessarily cover every possible scenario in SharePoint solution deployment, but it works. It takes three parameters: name of the SharePoint solution, web application name, and compatibility level of the solution. Script assumes that the wsp file is located in the same folder as the PowerShell. I know it's not perfect, but it can be easily modified to use full path to the wsp file instead.

param

(

[string]$WebApp = $null,

[string]$WSP_FileName = $null,

[string]$CompatibilityLevel = $null

)

 

function WaitForJobToFinish([string]$Identity)

{

$job = Get-SPTimerJob | ?{ $_.Name -like "*solution-deployment*$Identity*" }

$maxwait = 30

$currentwait = 0

if (!$job)

{

Write-Host -f Red '[ERROR] Timer job not found'

}

else

{

$jobName = $job.Name

Write-Host -NoNewLine "[WAIT] Waiting to finish job $jobName"

while (($currentwait -lt $maxwait))

{

Write-Host -f Green -NoNewLine .

$currentwait = $currentwait + 1

Start-Sleep -Seconds 2

if (!(Get-SPTimerJob $jobName)){

break;

}

}

Write-Host -f Green "...Done!"

}

}

 

function RetractSolution([string]$Identity, [string]$web_application, [string]$compatibility_level)

{

Write-Host "[RETRACT] Uninstalling $Identity"

Write-Host -NoNewLine "[RETRACT] Does $Identity contain any web application-specific resources to deploy?"

$solution = Get-SPSolution | where { $_.Name -match $Identity }

if($solution.ContainsWebApplicationResource)

{

Write-Host -f Yellow "...Yes!"

Write-Host -NoNewLine "[RETRACT] Uninstalling $Identity from $web_application web application"

Uninstall-SPSolution -identity $Identity -CompatibilityLevel $compatibility_level -WebApplication $web_application -Confirm:$false -ErrorVariable uninstallSolution

}

else

{

Write-Host -f Yellow "...No!"

Uninstall-SPSolution -identity $Identity -Confirm:$false

}

if ($uninstallSolution.Count -ne 0)

    {

        throw "Uninstalling SharePoint solution failed."

    }

else

{

Write-Host -f Green "...Done!"

}

WaitForJobToFinish

Write-Host -NoNewLine '[UNINSTALL] Removing solution:' $SolutionName

Remove-SPSolution -Identity $Identity -Force -Confirm:$false -ErrorVariable removeSolution

if ($removeSolution.Count -ne 0)

    {

        throw "Removing SharePoint solution failed."

    }

else

{

Write-Host -f Green "...Done!"

}

}

 

function DeploySolution([string]$Path, [string]$Identity, [string]$web_application, [string]$compatibility_level)

{

Write-Host -NoNewLine "[DEPLOY] Adding solution:" $Identity

Add-SPSolution $Path -ErrorVariable addSolution

if ($addSolution.Count -ne 0)

        {

            throw "Adding SharePoint solution failed."

        }

else

{

Write-Host -f Green "...Done!"

}

Write-Host -NoNewLine "[DEPLOY] Does $Identity contain any web application-specific resources to deploy?"

$solution = Get-SPSolution | where { $_.Name -match $Identity }

if($solution.ContainsWebApplicationResource)

{

Write-Host -f Yellow "...Yes!"

Write-Host -NoNewLine "[DEPLOY] Installing $Identity for $web_application web application"

Install-SPSolution -Identity $Identity -CompatibilityLevel $compatibility_level -WebApplication $web_application -GACDeployment -Force -ErrorAction Stop -ErrorVariable installSolution

}

else

{

Write-Host -f Yellow "...No!"

Write-Host -NoNewLine "[DEPLOY] Globally deploying $Identity"

Install-SPSolution -Identity $Identity -GACDeployment -Force -ErrorAction Stop -ErrorVariable installSolution

}

    if ($installSolution.Count -ne 0)

    {

        throw "Installing SharePoint solution failed."

    }

else

{

Write-Host -f Green "...Done!"

}

WaitForJobToFinish

}

 

$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }

if ($snapin -eq $null) {

Write-Host "[INIT] Loading SharePoint Powershell Snapin"

Add-PSSnapin "Microsoft.SharePoint.Powershell"

}

 

$identity = $WSP_FileName

$path = $WSP_FileName

Write-Host "[INFO] ----------------------------------------"

Write-Host "[INFO] Installing $Identity"

Write-Host -NoNewLine "[INFO] Determining if $Identity is already installed"

 

$isInstalled = Get-SPSolution | where { $_.Name -eq $identity }

if ($isInstalled)

{

Write-Host -ForegroundColor Yellow "...Yes!"

(RetractSolution $identity $WebApp $CompatibilityLevel)

(DeploySolution $path $identity $WebApp $CompatibilityLevel)

}

else

{

Write-Host -ForegroundColor Yellow "...No!"

(DeploySolution $path $identity $WebApp $CompatibilityLevel)

}

Write-Host -NoNewline "[INFO] Installation and deployment of $Identity"

Write-Host -ForegroundColor Green "...Done!"

 

##################################################################################

# Indicate the resulting exit code to the calling process.

if ($exitCode -gt 0)

{

"`nERROR: Operation failed with error code $exitCode."

}

"`nDone."

exit $exitCode

 

Here is a link to download the script.