Monday, December 19, 2011

Deploy WSP through PowerShell script in SharePoint 2010

In SharePoint 2010, we have power shell commands to do most of share point functionality.

Here is simple script to Uninstall-remove- add-install for any WSP


<#
Name       : WSPDeployment.ps1
Created by : Rajesh Soni
Created on : 19th Dec 2011
Purpose    : Deployment Script for WSPs,Before executing change all the veriables name defined at the top of ps script
#>

Add-PSSnapin Microsoft.SharePoint.Powershell

$WSPPath ="C:\WSPs\"
$WSPName="abc.wsp"
$WSPNameWithPath=$WSPPath +''+$WSPName
$SiteURL ="http://<URL Site>/"


function WaitForJobToFinish([string]$SolutionFileName)
    $JobName = "*solution-deployment*$SolutionFileName*"
    $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.."
    }
}


Write-Host "Uninstall Solutions "

Uninstall-SPSolution –Identity $WSPName -confirm:$false -WebApplication $SiteURL  
WaitForJobToFinish  $WSPName

Write-Host "Remove Solutions "
Remove-SPSolution –Identity $WSPName -confirm:$false


Write-Host "Add Solutions "

Add-SPSolution $WSPNameWithPath

Write-Host "Install Solutions "
Install-SPSolution –Identity $WSPName -Force -GACDeployment  -WebApplication $SiteURL  
WaitForJobToFinish  $WSPName

Write-Host -Fore Green "Successfully deployed"


Write-Host -Fore Green "List of solutions at server"

Get-SPSolution 

Remove-PsSnapin Microsoft.SharePoint.PowerShell

No comments:

Post a Comment