Sunday, July 6, 2014

PowerShell: Script to update solution

Solution deployment is one important task for SharePoint developer/admin, being it in local environment or any other Production/non-production farm.
Here is a script to automate this job and avoid errors. Only manual task here is to provide “Variable Values”, i.e. – name of WSP, path, web application URL etc.
In this case WSP is already added to farm solution store and deployed. We did some changes in existing code and need to update the solution. The Update-SPSolution cmdlet upgrades a deployed SharePoint solution in the farm. Use this cmdlet only if a new solution contains the same set of files and features as the deployed solution. If files and features are different, the solution must be retracted and redeployed.

1.       Copy the script below, save it as PS file, i.e. – UpdateSolution.ps1
2.       Edit it in any compatible editor; change Declaration of the Variables section as per your requirement.
3.       Open PS prompt/SharePoint management Shell with admin rights.
4.       Navigate to the folder where PS file is saved
5.       Execute the PS file as .\UpdateSolution.ps1


# ***********Script Start*********************************************
## Function – Solution Update
## Author – Deepak Solanki (deepaksinghsolanki@gmail.com)
## Version – 1.0
##
## Checks to ensure that Microsoft.SharePoint.Powershell is loaded, if not, adding pssnapin
## Checks if solution is already exists or deployed in Farm and give appropriate message
## The Update-SPSolution cmdlet upgrades a deployed SharePoint solution in the farm.
## Use this cmdlet only if a new solution contains the same set of files and features as the deployed solution.
## If files and features are different, the solution must be retracted and redeployed.
# ********************************************************************

 #########################Declaration of the Variables###############################
#Solution Name
$SolutionName = “MySolution.wsp”;
#Literal Path
$SolutionPath = “C:\Deployment\MySolution.wsp”;
############################ Declaration of the (helper) function   # ####################
Function UpdateSolution([string] $SolutionName, [string] $SolutionPath)
{  
if($SolutionName.ToLower().EndsWith(“.wsp”) -and $SolutionPath.ToLower().EndsWith(“.wsp”))  
{     
$count = 0     
##check to ensure that Microsoft.SharePoint.Powershell is loaded     
$snapin = get-pssnapin | where-object{$_.Name -eq “Microsoft.SharePoint.Powershell”}    if($snapin -eq $null) 
{     
write-host “Loading SharePoint snapin”     
add-pssnapin “Microsoft.SharePoint.Powershell”      } 
     ##check if WSP already exists in Farm     
$WSPExist = Get-SPSolution | Where {  ($SolutionName -eq $_.Name)} 
     if($WSPExist -eq $null)     
{        
Write-Host “$SolutionName Solution does not exist in solution store, can not be updated.” -ForegroundColor Red     
}     
elseif ((Get-SPSolution $SolutionName).Deployed)   
{    
Write-Host “$SolutionName Solution already exists in solution store and has been deployed. Starting update…” -ForegroundColor Green    
$SolutionTocheck = Get-SPSolution -identity $SolutionName    
if ($SolutionToCheck.ContainsGlobalAssembly)         
{     
Update-SPSolution -Identity $SolutionTocheck -LiteralPath $SolutionPath -GACDeployment    
}    
else    
{     
Update-SPSolution -Identity $SolutionTocheck -LiteralPath $SolutionPath    
}    
Write-Host “Operation completed.”  
 }     
else     
{      
Write-Host “$SolutionName Solution exists in solution store but has not been deployed, can not be updated” -ForegroundColor Red   
}  
}  
else  
{  Write-Host “Please provide a valid parameter” -ForegroundColor Red  }
}
############################Actual Function Call  # ###########################
UpdateSolution $SolutionName $SolutionPath;

# ***********Script End here*****************************


No comments:

Post a Comment