Pages

Search This Blog

Tuesday, February 1, 2011

Sharepoint 2010 Powershell Script for Installing/Removing WSP

Below are the commands written in powershell for retracting, removing, uninstalling and deploying the sharepoint solution. Copy the code and paste it in notepad and save with extension as .ps1. Now right click the file and click 'Run with powershell'.

Powershell script for Installing WSP:

# this function loads the powershell snapin in the command prompt.
function LoadSharePointPowerShellEnvironment
{
 write-host
 write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
 write-host
 Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
 write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function AddSolution($packageName, $packagePath)
{
 Write-Host
 Write-Host "Adding solution... $packageName..." -foregroundcolor Yellow
 $solution = Add-SPSolution $packagePath
}

function RetractSolution($packageName)
{
 Write-Host
 Write-Host "Retracting solution $packageName..." -foregroundcolor Yellow
 $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
  if ($solution -ne $null)
   {
   #Retract the solution
   if ($solution.Deployed)
   {
      if ($solution.ContainsWebApplicationResource)
      {
     $solution | Uninstall-SPSolution -AllWebApplications
      }
      else
      {
     $solution | Uninstall-SPSolution
      }
   }
  }
}

function RemoveSolution($packageName)
{
        Write-Host
  Write-Host "Removing solution $packageName..." -foregroundcolor Yellow
  Get-SPSolution $packageName | Remove-SPSolution
  Write-Host
  Write-Host "Solution Removed Sucessfully..." -foregroundcolor Green
}

function UpgradeSolution($packageName, $packagePath, $WebAppUrl)
{
    $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
   if (!$solution.ContainsWebApplicationResource)
   {
     Write-Host
  Write-Host "Deploying solution $packageName to the Farm..." -foregroundcolor Yellow
  $solution | Update-SPSolution -GACDeployment –LiteralPath $packagePath 
   }
   else
   {
  if ($WebAppUrl -eq $null -or $WebAppUrl.Length -eq 0)
  {
    Write-Warning "The solution $packageName contains web application resources but no web applications were specified to deploy to."
    Read-Host
    return
  }
  else
  {
    Write-Host
    Write-Host "Installing solution $packageName to WebAppUrl" -foregroundcolor Yellow
    $solution | Update-SPSolution -GACDeployment -WebApplication $WebAppUrl –LiteralPath $packagePath 
  }
   }
}

function DeploySolution($packageName, $packagePath, $WebAppUrl)
{
    $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
 if (!$solution.ContainsWebApplicationResource)
   {
     Write-Host
  Write-Host "Deploying solution $packageName to the Farm..." -foregroundcolor Yellow
  $solution | Install-SPSolution -GACDeployment
   }
   else
   {
  if ($WebAppUrl -eq $null -or $WebAppUrl.Length -eq 0)
  {
    Write-Warning "The solution $packageName contains web application resources but no web applications were specified to deploy to."
    Read-Host
    return
  }
  else
  {
    Write-Host
    Write-Host "Installing solution $packageName to WebAppUrl" -foregroundcolor Yellow
    $solution | Install-SPSolution -GACDeployment -WebApplication $WebAppUrl
  }
   }
}

# our main method
function ProcessInstallation($packageName, $packagePath, $WebAppUrl)
{
 $spAdminServiceName = "SPAdminV4"
 #check if solution already exists
 $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
   if ($solution -ne $null)
   {
     Write-Host
  Write-Host "The solution $packageName already exists."
  Write-Host
  Write-Host "Choose the following options:-"
  Write-Host "[r] Retract and Install solution"
  Write-Host "[u] Upgrade solution "
  Write-Host "[x] Exit"

  $readvalue = Read-Host
  if ($readvalue -eq "r")
  {
    #retract Solution
    RetractSolution $packageName

     #Remove Solution
    RemoveSolution $packageName
    #Add the solution
    AddSolution $packageName $packagePath
   
    #Deploy the solution
       DeploySolution $packageName $packagePath $WebAppUrl
  }
  elseif  ($readvalue -eq "u")
  {
    #upgrade the solution
    UpgradeSolution $packageName  $packagePath $WebAppUrl
  }
  else
  {
   return
  }
   }
   else
   {
    #Add the solution
    AddSolution $packageName $packagePath
   
    #Deploy the solution
       DeploySolution $packageName $packagePath $WebAppUrl
   }
  Write-Host
  Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
  Stop-Service -Name $spAdminServiceName
  Start-SPAdminJob -Verbose
  Start-Service -Name $spAdminServiceName
   #Block until we're sure the solution is deployed.
   do { Start-Sleep 2 } while (!((Get-SPSolution $packageName).Deployed))
  
   Write-Host
   Write-Host "Deployed Sucessfully... kindly do an IISRESET after the process. Press Enter to exit" -foregroundcolor Green
   Read-Host
}

#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#          Deploy the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#

#calling is done here.....

write-host
LoadSharePointPowerShellEnvironment
$currentDir = Get-Location
Write-host
[string]$packageName = "<SolutionName.wsp>"
[string]$packagePath = $currentDir.Path + "\" + $packageName
if (Test-Path $packagePath)
{
   write-host "-----------------------------------------------------------------------------"
   write-host "Installing package $packageName" -foregroundcolor Yellow
   write-host "-----------------------------------------------------------------------------"
}
else
{
   write-host
   write-host "No package found. Press any key to exit" - foregroundcolor Red
   Read-Host 
   return
}

Write-Host
write-host "Enter the Web Application Url where you need to deploy the solution"
[string]$WebAppUrl = Read-Host
$objSite = Get-SPSite $WebAppUrl -ErrorAction SilentlyContinue
 if($objSite -eq $null)
 {
   Write-Host
   Write-Host "Web Application Url not correct or Web application doesnot extsts. Re-run the exe with correct url." -foregroundcolor Red
   Read-Host
 }
 else
 {
  ProcessInstallation $packageName $packagePath $WebAppUrl
    }

____________________________________________________________________________

Powershell script for Removing WSP:

function LoadSharePointPowerShellEnvironment
{
 write-host
 write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
 write-host
 Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
 write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function RetractSolution($packageName)
{
  $spAdminServiceName = "SPAdminV4"
  $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
   if ($solution -ne $null)
   {
   #Retract the solution
   if ($solution.Deployed)
   {
     Write-Host
     Write-Host "Retracting solution $packageName..." -foregroundcolor Yellow
      if ($solution.ContainsWebApplicationResource)
      {
     $solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
      }
      else
      {
     $solution | Uninstall-SPSolution -Confirm:$false
      }
     Write-Host
     Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
     Stop-Service -Name $spAdminServiceName
     Start-SPAdminJob -Verbose
     Start-Service -Name $spAdminServiceName
     #Block until we're sure the solution is no longer deployed.
     do { Start-Sleep 2 } while ((Get-SPSolution $packageName).Deployed)
   }
  #Delete the solution
  Write-Host
  Write-Host "Removing solution $packageName..."
  Get-SPSolution $packageName | Remove-SPSolution -Confirm:$false
  Write-Host
  Write-Host "Solution Removed Sucessfully. kindly do an IISRESET after the process. Press Enter to exit" -foregroundcolor Green
     Read-Host
   }
   else
   {
     Write-Host
  Write-Host "No solution found to retract with name $packageName" -foregroundcolor Yellow
  Write-Host
  Write-Host "Press Enter to exit" -foregroundcolor Yellow
  Read-Host
   }
}

#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#          Retract the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#

LoadSharePointPowerShellEnvironment
Write-host "Retracting Solution..." -foregroundcolor Yellow
[string]$packageName = "<SolutionName.wsp>"
RetractSolution $packageName

No comments:

Post a Comment