Thursday, July 5, 2012

Install the SharePoint solution using PowerShell.


Hello today we will write the powershell script to install the wsp to the sharepoint solution store.

1. Open any text editor.

2. Copy the below lines of script code inside it and save the file as like "Install.ps1".

#********************************************************************************
Write-Host ''
Write-Host 'Checking whether the required services are running...' -foregroundcolor Yellow
#Start necessary services
#Start service of World Wide Web
$WorldWideWebService = Get-Service -Name W3SVC
if ($WorldWideWebService.Status -ne "Running")
{
#Start-SPAdminJob
Write-Host ''
Write-Host  'Starting  World Wide Web Service ....' -foregroundcolor Cyan
Start-Service W3SVC
}

#Start service of Sharepoint admin
$adminService = Get-Service -Name SPAdminV4
if ($adminService.Status -ne "Running")
{
#Start-SPAdminJob
Write-Host ''
Write-Host  'Starting  SharePoint 2010 Administration Service ....' -foregroundcolor Cyan
Start-Service SPAdminV4
}

#Start service of Sharepoint Timer
$timerService = Get-Service -Name SPTimerV4
if ($timerService.Status -ne "Running")
{
#Start-SPTimerV4
Write-Host ''
Write-Host  'Starting  SharePoint 2010 Timer Service ....' -foregroundcolor Cyan
Start-Service SPTimerV4
}

# Ensure SharePoint cmdlets are loaded :
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Add-PsSnapin Microsoft.SharePoint.PowerShell
}

# Variables
$CurrentDir= "D:\Solution Package"
$solutionNameCollection = @("Sharepointcustom.wsp")
$SiteUrl="http://server:port"

# Foreach loop to add and deploy the multiple solution to the solution store.
foreach ($solutionName in $solutionNameCollection)
{
$SolutionPath=$CurrentDir + "\"+$solutionName

function WaitForJobToFinish([string]$SolutionFileName)
{
$JobName = "*solution-deployment*$SolutionFileName*"
$job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
if ($job -eq $null)
{
Write-Host 'Timer job not found' -foregroundcolor Red
}
else
{
$JobFullName = $job.Name
Write-Host ''
Write-Host -NoNewLine 'Waiting to finish "'
Write-Host -NoNewLine $JobFullName -foregroundcolor Green
Write-Host -NoNewLine '" job ' -foregroundcolor DarkGray

while ((Get-SPTimerJob $JobFullName) -ne $null)
{
Write-Host -NoNewLine . -foregroundcolor DarkGray
Start-Sleep -Seconds 2
}

Write-Host -NoNewLine ' Done'  -foregroundcolor Green
Write-Host ''
}
}

Write-Host ''
Write-Host 'Checking whether the solution exists in the Solutions Store.' -foregroundcolor Yellow
$solution = Get-SPSolution $solutionName -ErrorAction:SilentlyContinue

if ($solution -ne $null)
{
Write-Host $solutionName 'Already exists'
Write-Host ''

Write-Host 'Checking if' $solutionName 'is already deployed' -foregroundcolor Yellow

# Check whether the solution is already deployed.
if ($solution.Deployed)      
{
Write-Host $solutionName "is already deployed"
Write-Host ''

Write-Host "Uninstalling " $solutionName -foregroundcolor Cyan

if ($solution.ContainsWebApplicationResource)
{
Write-Host "Retracting" $solutionName " from all web applications" -foregroundcolor Cyan
Uninstall-SPSolution -identity $solutionName -allwebapplication -confirm:$false
}
else {
Write-Host "Retracting" $solutionName -foregroundcolor Cyan
Uninstall-SPSolution -identity $solutionName -confirm:$false
}

#Write-Host 'Waiting for job to finish' -foregroundcolor DarkGray
WaitForJobToFinish $solutionName

Write-Host ''
}

# Remove solution from store.
Write-Host 'Removing ' $solutionName -foregroundcolor Cyan
Remove-SPSolution -identity $solutionName -confirm:$false -Force:$true
Write-Host ''
}

# Add solution to Solutions Store. :
Write-Host "Adding" $solutionName " to the Solutions Store" -foregroundcolor Cyan
Add-SPSolution -LiteralPath $solutionPath
#Add-SPSolution $SolutionPath
Write-Host ''

Write-Host "Deploying" $solutionName -foregroundcolor Cyan
# Can use -CASPolicies also here
# Not to use -AllWebApplications option.
# If the solution contains Web Application Resource, use this command.

Install-SPSolution –Identity $solutionName -WebApplication $SiteUrl –GACDeployment -Force:$true

#Note: If you want to install the wsp globally then replace the above line with below
#Install-SPSolution –Identity $solutionName –GACDeployment -Force:$true
}
Write-Host ''

WaitForJobToFinish $solutionName
Write-Host ''
Write-Host 'Done Deploying Solutions' -foregroundcolor Green


Write-Host ''
Write-Host "Resetting IIS to clear application pools" -foregroundcolor Cyan
iisreset /noforce
#********************************************************************************

3. You need to change the current directory path of the wsp inside the script .For that find the "$CurrentDir" and replace it with your existing wsp path.
i.e. if you existing wsp is located to "D:\Solution Package" then value of the $CurrentDir is
$CurrentDir="D:\Solution Package"

4. Once the solution path will modified then change name of your wsp package.For that find the $solutionNameCollection value and replace it with your existing wsp name.
   i.e.if your wsp name is like "Sharepointcustom.wsp" then the value of $solutionNameCollection is
   $solutionNameCollection = @("Sharepointcustom.wsp")
   Note: if tou want to install the multiple wsp to the sharepoint solution store then seperate the wsp with comaa(,)
i.e. $solutionNameCollection = @("Sharepointcustom1.wsp","Sharepointcustom2.wsp","Sharepointcustom3.wsp")

5. Now the time is to change your sharepoint WebApplication url on which you want to install the wsp.For that
   find the $SiteUrl and replace its value with your webapplication url.
   $SiteUrl="http://server:port"
 
6. Install-SPSolution –Identity $solutionName -WebApplication $SiteUrl –GACDeployment -Force:$true
   This line will deploy the wsp on specific web application.If you want to deploy the wsp Globally then folowing command will use.
   Install-SPSolution –Identity $solutionName –GACDeployment -Force:$true

7. Save all the changes and open the "SharePoint Management Shell"  as an administrator mode from
   All Program -> Microsoft Sharepoint 2010 Products -> SharePoint Management Shell
 
8. Change the direcory path with the existing wsp path that you have already set the value of $CurrentDir="D:\Solution Package" in the script.
   From the powershell command propmt type the following command and press enter.
   cd "D:\Solution Package"
 
9. Now type the following command and press enter.
   .\Install.ps1
 
10.Likewise powershell script is used to install the single or multiple wsp packages to the sharepoint solution store

Regards
Hiren Patel

3 comments:

  1. The next time I read a blog, I hope that it doesn't disappoint me as much as this one because you given only code but i want hole process. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

    ReplyDelete
  2. Hey that is a good post & a good set of conversations too. Keep sharing, that is good...

    ReplyDelete
  3. Awesome post Hiren. Way to go! Thanks a bunch........

    ReplyDelete