Deploying SharePoint 2010 Solution with PowerShell

Put together a small script that to help automate deployment of a solution.  The script handles the following:

  • Install and Deploy a farm level solution
  • Activate a web application scoped feature
  • Activate a site collection scoped feature

The tricky part with scripting this occurs when the solution is deployed [Install-SPSolution cmdlet].  The script needs to wait for the deployment to finish before activating the features.  Without waiting, the script will immediately continue, and try to activate the features that have not been deployed yet. That means evil red text will appear complaining about features not existing in the farm.

Here’s a copy of the script.  There are some things hard coded in this sample.  The name of the solution package on the Install-SPSolution line, and the Feature ID’s on the Enable-SPFeature lines.

    1: param($wspPath, $url)
    2:  
    3: "`r`nBegin : Installing and deploying solution package from $wspPath"
    4:  
    5:  
    6:     "`r`n`tBegin : Adding Solution to farm solution store"
    7:  
    8:         #Add the WSP to the solution store
    9:         Add-SPSolution "$wspPath" | Out-Null
   10:  
   11:     "`tEnd : Adding Solution to farm solution store"
   12:  
   13:  
   14:     "`r`n`tBegin : Deployng the solution package"
   15:  
   16:         #Deploy the solution
   17:         Install-SPSolution MySolutionPackage.wsp -GacDeployment
   18:  
   19:         #Wait for the solution to deploy prior to continuing
   20:         while((Get-SPSolution MySolutionPackage.wsp).Deployed -eq $false)
   21:         {
   22:             Start-Sleep -seconds 30
   23:         }
   24:  
   25:     "`tEnd : Deployng the solution package"
   26:  
   27:  
   28:  
   29:     "`r`n`tBegin : Enabling Web Application Feature"
   30:  
   31:         #Enable Web Application Feature
   32:         Enable-SPFeature 316883d1-01f8-4474-8e0d-e5e32cecfc2c -url $url -force
   33:  
   34:     "`tEnd : Enabling Web Application Feature"
   35:  
   36:  
   37:     "`r`n`tBegin : Enabling Site Collection Feature"
   38:  
   39:         #Enable Site Collection Feature
   40:         Enable-SPFeature b47afc1d-3369-4399-9fde-15ff0ade7316 -url $url -force
   41:  
   42:     "`tEnd : Enabling Site Collection Feature"
   43:  
   44:     #"`tBegin : Setting Web Application Properties"
   45:     #
   46:     #    #Set the property "MyProperty"
   47:     #    Get-SPWebApplication $url | %{$_.Properties["MyProperty"] = "Some Value"; $_.Update()}
   48:     #    
   49:     #    #Retrieve the property "MyProperty"
   50:     #    Get-SPWebApplication $url | %{$_.Properties["MyProperty"] }
   51:     #
   52:     #"`tEnd : Setting Web Application Properties"
   53:  
   54:  
   55: "`r`nEnd : Installing and deploying solution package from $wspPath`r`n"