Tuesday, May 2, 2017

Sharepoint – Powershell–Deployment script for sharepoint solutions

 

Steps)

       1) Save below scrips in to spdeploy.ps1

       2) Open Sharepoint Manangement shell and run below command.

.\ spdeploy.ps1 –solutionName solution Name-location Location to solution file

 

param(
    [Parameter(Position=0, Mandatory="True",
        HelpMessage="Need the script filename to be tested.")]
    [string]
    # The Script to test
    $solutionName,
    [Parameter(Position=1, Mandatory="True",
        HelpMessage="Need the config filename for this Farm.")]
    [string]
    # Farm Config XML File
    $location
)   

$location = $location

$solutionName = $solutionName +'.wsp'

function solutionStatus([string]$solutionName ,[string]$location)
{
    try
    {
        $s = Get-SPSolution | where-object {$_.Name -eq $solutionName}
            if ($s -ne $null)
              {
                if ($s.Deployed -eq $true -And $s.JobExists -eq $False)
                   {
                     Write-Host -NoNewline $solutionName " found - "  -ForegroundColor Yellow
                     Write-Host -ForegroundColor Green "Updating $solutionName "
                     spUpdate $solutionName $location
                     WaitForSPSolutionJobToComplete $solutionName
                   }
                   else
                   {
                   Write-Host -NoNewline  $solutionName " found - Solution status - not deployed  "  -ForegroundColor Yellow
                   spRemove $solutionName $location
                   Write-Host -ForegroundColor Green "Removing $solutionName "
                   spInstall $solutionName $location
                   WaitForSPSolutionJobToComplete $solutionName
                   }
               }
             else
               {
                 Write-Host -NoNewline $solutionName " not found - "  -ForegroundColor Yellow
                 spInstall $solutionName $location
                 WaitForSPSolutionJobToComplete $solutionName
               }
               Write-Host  $solutionName " successfully deployed"  -ForegroundColor Green
               Write-host  "--------------------------------------------" -ForegroundColor Green
               $s.DeployedServers
        
         }
         catch
         {
          Write-Host  $_.Exception.Message  -ForegroundColor Red
         }
         
}

function spInstall([string]$solutionName ,[string]$location)
{
    $solution = Add-SPSolution -LiteralPath $location'\'$solutionName
         if ($solution.ContainsWebApplicationResource)
            {
            Write-Host -ForegroundColor Green "Deploying $solutionName "
            Install-SPSolution -Identity $solutionName -AllWebApplications -GACDeployment
            }
        else
            {
               Write-Host -ForegroundColor Green "Deploying $solutionName"
               Install-SPSolution -Identity $solutionName -GACDeployment
            }
}

function spUpdate([string]$solutionName ,[string]$location)
{
    Update-SPSolution -Identity $solutionName -LiteralPath $location'\'$solutionName -GACDeployment
}

function spUnInstall([string]$solutionName ,[string]$location)
{
   
}

function spRemove([string]$solutionName ,[string]$location)
{
Remove-SPSolution -Identity $solutionName -Confirm:$false
}

function WaitForSPSolutionJobToComplete([string]$solutionName)
{
    $solution = Get-SPSolution -Identity $solutionName -ErrorAction SilentlyContinue
 
    if ($solution)
    {
        if ($solution.JobExists)
        {
            Write-Host -NoNewLine "Waiting for solution to deploy :- " -ForegroundColor Yellow
            Write-Host  $solutionName -ForegroundColor Red
 
        }
        
        # Check if there is a timer job still associated with this solution and wait until it has finished
        while ($solution.JobExists)
        {
            $jobStatus = $solution.JobStatus
            
            # If the timer job succeeded then proceed
            if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Succeeded)
            {
                Write-Host "Solution '$solutionName' timer job suceeded"
                return $true
            }
            
            # If the timer job failed or was aborted then fail
            if ($jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Aborted -or
                $jobStatus -eq [Microsoft.SharePoint.Administration.SPRunningJobStatus]::Failed)
            {
                Write-Host "Solution '$solutionName' has timer job status '$jobStatus'."
                return $false
            }
            
            Write-Host -NoNewLine "Waiting for solution to deploy :- " -ForegroundColor Yellow
            Write-Host  $solutionName -ForegroundColor Red
            Sleep 1
        }
        
        # Write a new line to the end of the '.....'
        Write-Host
    }
   
  
}

solutionStatus $solutionName $location

No comments:

Post a Comment