Sharepoint 2010 deploy script

I used to work with SharePoint and it gave me some of the most frustrating days of my life. On the plus side, I've been complimented on my "tenacious" approach to problem solving that I'm fairly sure I can give SharePoint the credit for creating.

One of the most annoying things I found about SP was deploying packages. I used to hand off a .wsp to the infrastructure team who would valiantly go off and deploy it. As soon as something went wrong, they'd just come back and ask for help - they had enough to deal with without having to learn SharePoint's nuances. In order to help them, I created a simple script that was mostly fire and forget.

The code is available as a gist it's only designed to work one feature at a time but I think that should be easy enough to modify.

#############################################################################################
#         This script deploys a feature to a SharePoint 2010 farm.                          #
#############################################################################################
 
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ea "SilentlyContinue"
 
$packageName = "XXX.wsp"        # The name of the package to deploy
$targetDirectory = "C:\XXX"     # The directory that contains the package to deploy
$siteUrl = "XXX"                # The url of the site/site collection the feature is deployed to
$webApp = "XXX"                 # The name of the application or the GUID of the application
$featureName = "XXX"            # The name of the feature 
 
#############################################################################################
#         Waits for the solution deployment timer job to finish.                            #
#############################################################################################
Function Wait-TimerJob 
{
  [CmdletBinding()] 
  param ()
  $job = Get-SPTimerJob | ?{ $_.Name -like "*solution-deployment*" }
  if ($job -eq $null) 
  {
    Write-Host "Solution deployment job not running." 
  }
  else
  { 
    # If it's taking too long, throw an error so problem can be investigated.
    $timeOut = (Get-Date).AddMinutes(5)
    while ((Get-SPTimerJob $job.Name) -ne $null) 
    {
      Write-Host -NoNewLine .
      Start-Sleep -Seconds 2
      if ((Get-Date) -gt $timeOut)
      {
        $message = "Operation timed out while waiting for the solution deployment timer job to complete."
        $exception = New-Object System.OperationCanceledException $message
        $errorId = "TimeOutWaitingForJob"
        $errorCategory = "OperationTimeout"
        $target = "Wait-TimerJob"
        $errorRecord1 = New-Object Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $target
        $PSCmdlet.ThrowTerminatingError($errorRecord1)
      }
    }
    Write-Host ""
  }
}
 
if ((Get-SPFeature -Site $siteUrl -Identity $featureName -EA SilentlyContinue) -ne $null)
{
  Write-Host "Disabling feature..." 
  Disable-SPFeature $featureName -Url $siteUrl  -force -confirm:$false
  Write-Host "Feature disabled." 
}
 
$solutionRef = Get-SPSolution -Identity $packageName -EA SilentlyContinue
if ($solutionRef -ne $null)
{
  if ($solutionRef.Deployed)
  {
    Write-Host "Uninstalling solution..." 
    Uninstall-SPSolution –Identity $packageName –WebApplication $webApp  -confirm:$false 
    Wait-TimerJob
    Write-Host "Solution uninstalled." 
  }
  Write-Host "Removing solution..." 
  Remove-SPSolution –Identity $packageName  -confirm:$false 
  Write-Host "Solution removed." 
}
 
Write-Host "Adding new solution..." 
$result = Add-SPSolution $targetDirectory\$packageName
Write-Host "Solution added."  
 
Write-Host "Installing solution..." 
Install-SPSolution –Identity $packageName –WebApplication $webApp  -GACDeployment -CASPolicies -Force
Wait-TimerJob
Write-Host "Solution installed." 
 
Write-Host "Enabling feature..." 
Enable-SPFeature $featureName -Url $siteUrl  -force
Write-Host "Feature enabled." 

Comments

Popular posts from this blog

Trimming strings in action parameters in ASP.Net Web API

Full text search in Entity Framework 6 using command interception

Composing Expressions in C#