Tuesday, 29 July 2014

Deploy Sharepoint Solution using powershell script. This powershell script will deploy the solution globally..

## check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the
SharePoint Management Shell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq
'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{   
 Write-Host "Loading SharePoint Powershell Snapin"   
 Add-PSSnapin "Microsoft.SharePoint.Powershell"
}


## Read the Farm parameters
$SolutionName = "SolutionName1.wsp"
## Store current directory
$CurrentDirectory= get-location
$SolutionsFolder = $CurrentDirectory
$Scope="Global"
## Create the log file at the desired location with current date and time format.
$LogFileName =  "C:\Logs\DeploySolutionName-$(get-date -format MMddyyHHmmss).log"
Set-Content -Path $LogFileName "DeploySolution Script Execution starts at $(get-
date -format MM-dd-yyyy-HH:mm:ss) ...."
# Log generation.
function Write-Log
{
param ([string]$info, [bool]$logReq)
Add-Content -Path $LogFileName -value $info  
   if($logReq )
   {
     Write-Host $info
   }
}

## this method will delete and retract the solution.
function DeleteSolution
{
 param ([string]$solutionName)
    $solution = (Get-SPFarm).Solutions[$solutionName]
   
    #Check if solution is present in the solution store or not.
 if($solution)
 { 
        Write-Log -info "Initiating to uninstall $solutionName..." -logReq $true
  if( $solution.Deployed -eq $true )
  {
   Uninstall-SPSolution -Identity $solutionName -Confirm:1
  
   while( $solution.JobExists )
   {   
                Write-Log -info "Waiting for retraction of $solutionName..." -
logReq $true
    sleep 5
   }
  }
 
        Write-Log -info "Initiating the removal of $solutionName..." -logReq $true
  Remove-SPSolution -Identity $solutionName -Force -Confirm:1
 
        Write-Log -info "$solutionName is deleted from this farm." -logReq $true
 }
 else
 { 
        Write-Log -info "$solutionName not found in the solution store." -logReq
$true
 }
}
## This method installs and deploys a solution based on its solution id and path.
function DeploySolution
{
 param ([string]$solutionName, [string]$solutionPath)
 $filename = $solutionPath + "\" + $solutionName

    Write-Log -info "Initiating to add solution $solutionName from $filename
globally..." -logReq $true
 Add-SPSolution -LiteralPath $filename

    Write-Log -info "$solutionName added successfully to the solution store." -
logReq $true

    Write-Log -info "Initiating the installation of $solutionName globally." -
logReq $true

 Install-SPSolution –Identity $solutionName -GACDeployment -Force
   
    $solution = (Get-SPFarm).Solutions[$solutionName]

 if((Get-SPFarm).Solutions[$solutionName])
 { 
        while( $solution.Deployed -eq $false )
  {
   try
   {   
                Write-Log -info "Waiting for installation of $solutionName..." -
logReq $true
    sleep 5
   }
   catch
            {             
              Write-Log -info "Installation of $solutionName has failed. Please see
central admin for more details." -logReq $true
              Write-Log -info $_ -logReq $true
            }
  }
 
        Write-Log -info "$solutionName is installed successfully in the farm." -
logReq $true
 }
 else
 { 
        Write-Log -info "$solutionName not found in the solution store." -logReq
$true
 }
}
##Start Administration Service.
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
     $IsAdminServiceWasRunning = $false;
     Start-Service $AdminServiceName  
     Write-Log -info "SharePoint 2010 Administration service was stopped, so
started now." -logReq $true
}
##Restart Timer Service
Write-Log -info "Attempting to Start SharePoint 2010 Timer Service, if not
running." -logReq $true
Start-Service "SPTimerV4"
$spTimerService = Get-Service "SPTimerV4"
while($spTimerService.Status -ne "Running")
{
    Start-Sleep -Seconds 20
    Start-Service "SPTimerV4"
    $spTimerService = Get-Service "SPTimerV4"
}
Write-Log -info "SharePoint 2010 Timer Service is running now." -logReq $true

## start retract, delete, install and deploy solution.
if($Scope -eq "Global")
{
   DeleteSolution -solutionName $SolutionName
   DeploySolution -solutionName $SolutionName -solutionPath $SolutionsFolder
}
## IISReset
IISRESET
##Restart Timer Service
Write-Log -info "Attempting to Start SharePoint 2010 Timer Service, if not
running." -logReq $true
Start-Service "SPTimerV4"
$spTimerService = Get-Service "SPTimerV4"
while($spTimerService.Status -ne "Running")
{
    Start-Sleep -Seconds 20
    Start-Service "SPTimerV4"
    $spTimerService = Get-Service "SPTimerV4"
}
Write-Log -info "SharePoint 2010 Timer Service is running now." -logReq $true
Write-Log -info "DeploySolution Script Execution completed at $(get-date -format
MM-dd-yyyy-HH:mm:ss) ...." -logReq $true

No comments:

Post a Comment