Scripting WSP deployment

I won’t take this opportunity to evangelise the benefits of scripted deployments, I’m going to assume that you already do it (as you should be!) and provide a tiny bit of script that will identify when your deployment doesn’t run quite so smoothly. Before I do, I’ll briefly mention my experience as to why a deployment may fail.

I have found that the by far the primary reason that a SharePoint full-trust deployment fails is that one or more of the assemblies being deployed is locked in the GAC and cannot be removed/replaced. An ISSRESET fixes this in the majority of cases (consider performing a remote ISSRESET across all the farm SharePoint servers as part of your deployment process. Note to self: future blog topic…) and in the remainder of cases stopping related services (v4 Timer, SSRS etc) on the affected server will release the assembly. The easiest way to identify the servers at which the deployment failed is via CA:

Central Administration > System Settings > Farm Management :: Manage farm solutions > "Your WSP"
Central Administration > System Settings > Farm Management :: Manage farm solutions > “Your WSP”

WSPs are deployed using a timer job. When performing deployment actions we need to wait on the timer job and upon it completing then verify the deployment status of the solution is as we expect. If we don’t wait for the action and ensure that it has run successfully we run this risk of not detecting a failed to deployment until we attempt to access the site. This can be a real time sink if we run lengthy scripts after deployment or are scripting the deployment a number of solutions in succession. The following snippet shows how easy it is to achieve this in PowerShell.

# Define function to wait for solution job to end
function WaitOnSPSolutionJob($solution) 
  while ($solution.JobExists)	
    write-host "." -nonewline
    sleep 4
    $solution = Get-SPSolution $solution.Name -ea SilentlyContinue
  return $solution

# Uninstall solution
$solution = Get-SPSolution $solutionName -ea SilentlyContinue
if($solution -ne $null)
  $solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
  Remove-SPSolution -Identity $solutionName -Confirm:$false
  $solution = WaitOnSPSolutionJob($solution)
    throw "Failed to remove solution"

# Deploy Solution
$solution = Add-SPSolution "$solutionPath\$solutionName"
Install-SPSolution $solutionName `
  -WebApplication $webApplication `
  -GACDeployment:$($solution.ContainsGlobalAssembly) `
  -CASPolicies:$($solution.ContainsCasPolicy) -force
$solution = WaitOnSPSolutionJob($solution)
  throw "Failed to deploy solution"

May your deployment failures be immediately detected.

Leave a Reply

Your email address will not be published.