2013-06-06

Here is a Powershell Script to that deploys a WSP and activates a SharePoint feature

$solutionName="solutionname.wsp"
$featureName="featurename"
$SolutionPath="C:\wsp\"+$solutionName

if($args[0] -eq 'DEV') {
$webAppName="http://url"
}
elseif ($args[0] -eq 'QA') {
$webAppName="http://url"
}
elseif ($args[0] -eq 'PROD') {
$webAppName="http://url"
}
else {
Write-Host "$(Get-Date -Format o) Environment not specified - please pass appropriate environment to deploy -"
exit
}

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.."
}
}

$powershellSnapin = "Microsoft.Sharepoint.Powershell"

if ((Get-PSSnapin -Name $powershellSnapin -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "--------------------------------- Adding sharpoint powershell snapin if not existing-------------------------"
Add-PsSnapin $powershellSnapin
}

if($DeployWebURL -eq '')
{
Write-Host "$(Get-Date -Format o) Incorrect Environment specified - please pass 'DEV/QA/PROD' -"
exit
}

Write-Host 'Going to disable feature ' $featureName
disable-spfeature -identity $featureName -confirm:$false -url $webAppName

Write-Host 'Going to uninstall feature ' $featureName
uninstall-spfeature -identity $featureName -confirm:$false -force

Write-Host 'Going to uninstall solution '$solutionName
Uninstall-SPSolution -identity $solutionName -confirm:$false

Write-Host 'Waiting for job to finish'
WaitForJobToFinish

Write-Host 'Going to remove solution'
Remove-SPSolution –identity $solutionName -confirm:$false

Write-Host 'Going to add solution '$SolutionPath
Add-SPSolution $SolutionPath

Write-Host 'Going to install solution to web application '$solutionName
Install-SPSolution –identity $solutionName –GACDeployment

do
{
Write-Host “.” -NoNewline -ForeGroundColor Green;
Start-Sleep -Seconds 5; 
try
{
write-host 'Verifying if the solution installation is valid'
$testsolution = Get-SPSolution -Identity $solutionName
}
catch
{}
}while(!$testsolution);

Write-Host 'Going to enable Feature ' $featureName
Enable-spfeature -identity $featureName -confirm:$false -url $webAppName
Write-Host 'Enabled Feature' $featureName
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Save the PS as Deploy.ps1

Change the Parameters required for :

  • $solutionName=”solutionname.wsp”
  • $featureName=”featurename” and
  • $webAppName  accordingly and deploy the commands via .\Deploy.ps1 Dev or .\Deploy QA or .\Deploy.ps1 Prod

About the author 

Jayaraja Jayaraman