SharePoint 2010 – Powershell Script to Automatically deploy WSP packages

SharePoint 2010 –  Powershell Script to Automatically deploy WSP packages

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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s