Wednesday 9 April 2014

Installing and Deploying the wsp in a single click with Powershell script


Although we can install and deploy the wsp solutions using the powershell commands, but each time we deploy them, we need to remember the commands and give KT to the people who are deploying them for the first time.

Keeping all those in mind, I have created powershell script and a batch file. User just need to copy them to the server where they want to install or deploy and click the .bat file. That's it!

Steps to Install and Deploy Solution for the First time

  • Open the notepad and copy the below script and save the file as DeployNewSolution.ps1
       Script code:
           
1:  Add-PsSnapin Microsoft.SharePoint.PowerShell  
2:        $wspFileName= read-host "Enter the wsp package name"  
3:        $Path = read-host "Enter the wsp file Location Ex:D:/Solutions/"  
4:        $URL = read-host "Enter the url of the application to deploy Ex:testapp.test.com"  
5:        $filePath = "$Path$wspFileName"  
6:       #$filePath = "F:/test/DeploymentScripts/$wspFileName"  
7:       if($wspFileName -ne "")  
8:       {  
9:         try  
10:        {  
11:          write-host "Adding Solution to Solution Gallary......"  
12:          Add-SPSolution "$filePath"  
13:          write-host "Solution added successfully"  
14:        }  
15:        catch  
16:        {  
17:         write-host "Error occured while adding the solution to Gallery"  
18:        }  
19:      }  
20:     if($wspFileName -ne "")  
21:     {          
22:       [string] $wspFileName = [System.Convert]::ToString($wspFileName )  
23:       [string] $URL = [System.Convert]::ToString($URL)  
24:       .".\DeploySolution.ps1"  
25:      DeploySolution $wspFileName $URL            
26:    }  
27:    Remove-PsSnapin Microsoft.SharePoint.PowerShell  

  • Create another .ps1 file which includes a function for Deploying the script and name it as "DeploySolution.ps1"
  • Copy the below script code to the file
Script Code:
 
1:  Function DeploySolution([string]$wspFileName, [string]$URL)  
2:  {  
3:  #$wspFileName= read-host "Enter the wsp package name to deploy"  
4:  #$URL = read-host "Enter the url of the application to deploy"  
5:  if($wspFileName -ne "")  
6:  {  
7:    try  
8:    {  
9:      write-host "Deploying the solution......"  
10:      Install-SPSolution -Identity $wspFileName –WebApplication $URL -GACDeployment -force  
11:      write-host "Solution deployed successfully"  
12:    }  
13:    catch  
14:    {  
15:      Write-Error "Error occured while deploying the wsp package"  
16:    }  
17:  }  
18:  }  
  • Create a batch file with name "DeployNewSolution.bat" and copy the below content to it.
1:  cd /d %~dp0  
2:  powershell -noexit -file  ".\DeployNewSolution.ps1" "%CD%"  
3:  pause  

  • Copy the above 3 files created above to the server where you want to deploy and click the DeployNewSolution.bat file from where the execution starts.
 Note:   We don't need to do the above process each time we deploy a different wsp, rather we can use the same files but we need to enter the required names and paths when it is prompted for wsp names and application where it need to be installed.
 

No comments:

Post a Comment