Share via


Powershell Script to Automatically Deploy Sharepoint WSP Packages

Recently I wanted to automate the retraction, removal and deployment of WSP packages in a folder. So I wrote this powershell script.

$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 "Expected: Packaged not installed"
    } 

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