Powershell Script to Automatically Deploy Sharepoint WSP Packages

Recently I wanted to automate the retraction, removal and deployment of WSP packages similar to how Visual Studio does it.  And I wanted to be able to just put a solution WSP file in a folder with the script, and run it without any configuration or batch files.  So I searched Google and found a nice script on TechNet.  But when I ran it, it failed.  I made some modifications to the script and here is final result:

$packages = (dir *.wsp | Select-Object name) 
$currentDir = (Get-Location).Path 
Add-PSSnapin Microsoft.Sharepoint.PowerShell -ErrorAction "SilentlyContinue"
Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.
Write-Host "Started package installation"

function WaitForJobToFinish ([string]$solutionName)   
{
     $JobName = "solution-deployment-$solutionName*"   
     $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }   
 if ($job -eq $null)    
 {
         Write-Host "Timer job not found"
 }else
 {
  $JobFullName = $job.Name       
  Write-Host -NoNewLine "Waiting to finish job $JobFullName"           
  while ((Get-SPTimerJob $JobFullName) -ne $null)        
  {
   Write-Host -NoNewLine .
          Start-Sleep -Seconds 2       
  }
  Write-Host  "Finished waiting for job.."   
 }
}

foreach ($i in $packages) 

    Write-Host -NoNewLine "Waiting for deployment jobs to finish..."           
    while ((Get-SPTimerJob  | ?{$_.Name -like "solution-deployment*"}) -ne $null)        
    {
     Write-Host -NoNewLine .
     Start-Sleep -Seconds 2       
    }
    Write-Host  "Finished waiting for job.."   
    Write-Host "Retracting: " + $i 
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    Write-Host $solution.Name 
    if ($solution -ne $null) 
    { 
      Write-Host "Solution Found..."
     if ($solution.Deployed -eq $true) 
     { 
      Write-Host "Uninstalling..."
      try
      {
       Uninstall-SPSolution -Identity $i.Name -AllWebApplications -Confirm:$false
      }catch
      {
       Uninstall-SPSolution -Identity $i.Name -Confirm:$false 
      }
     }
     Write-Host "Retract Completed!"
     Write-Host "Removing Solution..."
     do{
  Write-Host -NoNewLine .
             Start-Sleep -Seconds 2
     }
     until ($solution.Deployed -eq $false)
     WaitForJobToFinish $i.Name
            Start-Sleep -Seconds 5
     Remove-SPSolution -Identity $i.Name -Force -Confirm:$false
     Write-Host "Remove Completed!"
    } 
    else
    { 
     Write-Host "WSP Package was not previously installed, moving to next step"
    } 

foreach ($i in $packages)

    Write-Host "Deploying: " + $i 
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    if ($solution -eq $null) 
    { 
     Write-Host "Adding Solution..."
     $solution = Add-SpSolution -LiteralPath ($currentDir + "\" + $i.Name) 
     WaitForJobToFinish $solution.Name
     Write-Host "Deployment Completed!"
     Write-Host "Installing Solution..."
     try{
  Write-Host "Installing for web application(s)"
         Install-SPSolution -Identity $solution.Name -allwebapplications -GACDeployment -CASPolicies
      }
     catch
     {
  Install-SPSolution -Identity $solution.Name -GACDeployment -CASPolicies
     }
    } 

Stop-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.

Remove-PsSnapin Microsoft.SharePoint.Powershell

Put this script in a .ps1 file (I named mine deployWSP.ps1).  Then put this file and your solution WSP file in the same folder.  Open PowerShell as Administrator, change directory to where you just put the 2 files.  Now run script - in my example, I typed this at the command line:

.\deployWSP.ps1


Permalink | Comments (0) | Post RSSRSS comment feed