Navigate Up
Sign In
Supporters of Developer
Web

Deploying your solution with PowerShell - Part 1

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

 

The last couple of weeks I’ve been quite busy with 2 projects. For both projects I have written a PowerShell script for deploying the solutions and provisioning the SharePoint sites. In this series of PowerShell posts I‘d like to share my PowerShell scripts.

For starters, the scripts are not just for the final deployment, but they are very useful and time saving (!) during development and test. How many times have you had to start over again, deleting and creating the site collection; activated one more feature to test and create a web site along with some lists, etc… ? You do not (I repeat YOU DO NOT) want that to do it many times a day. PowerShell to the rescue!

So, let’s begin. We'll start with 2 scripts, one for deploying and one for removing your solution. I use PowerGUI as the tool for writing and debugging PowerShell Scripts. I cannot live without it anymore.

The Deploy.ps1 script file contains the following code:

param([string]$url="http://intranet.octavie.local") 
  
$solutionName = "Octavie.SharePoint.Intranet.wsp"
$solutionFile = ".\$solutionName"
  
$solution = Add-SPSolution $solutionFile
  
if ( $solution.ContainsWebApplicationResource ) { 
    Write-Host -ForegroundColor Green "Deploying $solutionName to $url"
    Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force -Webapplication $url
} 
else { 
    Write-Host -ForegroundColor Green "Deploying $solutionName"
    Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force
} 
  
$solution = Get-SPSolution $solutionName
  
if ($solution.Deployed -eq $false ) { 
    $counter = 1 
    $maximum = 50 
    $sleeptime = 2 
    while( ($solution.JobExists -eq $true ) -and ( $counter -lt $maximum  ) ) { 
        write-host -ForegroundColor yellow "Please wait..."
        sleep $sleeptime
        $counter++ 
    } 
} 
  
Write-Host ""
Write-Host -ForegroundColor Green "$solutionName is deployed"

Three important cmdlets are used here:

  • Add-SPSolution : this adds your WSP to the Farm’s Solutions
  • Install-SPSolution: this deploys your solution to one or more web applications
  • Get-SPSolution : this gets a SPSolution object, so we can check if the solution is already deployed. Hence, the Deployed and JobExists property.

The Remove.ps1 script file contains the following code:

param([string]$url="http://intranet.octavie.local") 
  
$solutionName = "Octavie.SharePoint.Intranet.wsp"
  
$solution = Get-SPSolution $solutionName
  
Write-Host -ForegroundColor Green "Removing $solutionName"
Write-Host ""
  
if( $solution.Deployed -eq $true) { 
  
    if ( $solution.ContainsWebApplicationResource ) { 
        Uninstall-SPSolution -Identity $solutionName -Confirm:$false -Webapplication $url
    } 
    else { 
        Uninstall-SPSolution -Identity $solutionName -Confirm:$false
    } 
    $counter = 1 
    $maximum = 50 
    $sleeptime = 2 
    while( $solution.JobExists -and ( $counter -lt $maximum ) ) { 
        Write-Host -ForegroundColor yellow "Retracting $solutionName. Please wait..."
        sleep $sleeptime
        $counter++ 
    } 
} 
  
Write-Host ""
Write-Host -ForegroundColor Green "$solutionName is retracted."
Write-Host ""
  
Remove-SPSolution -Identity $solutionName -Force -Confirm:$false
  
Write-Host -ForegroundColor Green "$solutionName removed."

In the next post I will cover creating a site collection and provisioining it with some content. Then I will also use a refactored script for deploying and removing solutions.

Categories: PowerShell

Comments

Olaf Didszun

Why not a generic script

Very simple scripts, but it does all a developer (and even an ITpro) needs for deployment. But I would prefer to have generic scripts for the deployment process, and these scripts get their parameters from an external file.

Posted 07-Jul-2011 by Olaf Didszun

CodePlex Equivalent

Hi Olaf,
 
Been a while!  There is a version on CodePlex which does pull parameters from an external xml file, as well as allows additional modularisation.  It's pretty good.
 
I also like Octavie's work as a good starter for those that are learning PowerShell or don't need to fully understand what they'rr running.

Posted 08-Jul-2011 by
Olaf Didszun

Where do I find the CodePlex project

Hi Steven,
 
do you have the link to the CodePlex project?

Posted 08-Jul-2011 by Olaf Didszun

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: