July 12, 2012

SharePoint PowerShell Script: Deploy WSP Solution

# Created by Dennis Alzoubi    
# Version 1.1
# Last Updated 6/18/2012 8:34 am
# Purpose: Deploys a new solution to the specified web application. If solution already exists, it updates it.
param([Parameter(Position=0, Mandatory=$true, HelpMessage="Url of Web Application. 'All' for All Web Applications)")][string]$WebAppUrl, 
      [Parameter(Position=1, Mandatory=$true, HelpMessage="Name of Solution")][string]$WspName)
 
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Write-Host 'Reading in SharePoint SnapIn'
    Add-PsSnapin Microsoft.SharePoint.PowerShell;
}
 
function WaitForSPSolutionJobToComplete([string]$solutionName)
{
    $solution = Get-SPSolution -Identity $solutionName -ErrorAction SilentlyContinue
    if ($solution)
    {
        if ($solution.JobExists)
        {
            Write-Host -NoNewLine "Waiting for timer job to complete for solution '$solutionName'."
        }
        # 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
            }    
            # Otherwise wait for the timer job to finish
            Write-Host -NoNewLine "."
            Sleep 1
        }    
        # Write a new line to the end of the '.....'
        Write-Host
    }    
    return $true
}
 
Write-Host 'Checking to see if solution is already deployed'
$farm = Get-SPFarm
$solutions = $farm.Solutions
$exists = $false
 
foreach ($solution in $solutions)
{
    if ($solution.Name -eq $WspName)
    {
        $exists = $true
        break
    }
}
 
if ($exists)
{
    $location = Get-Location
    Write-Host Updating $WspName
    Update-SPSolution -Identity $WspName -LiteralPath "$location\$WspName" -GACDeploym
}
else
{    
    Write-Host Adding $WspName
    Add-SPSolution -LiteralPath $WspName
    
    Write-Host Installing $WspName
    if ($WebAppUrl -eq 'All')
    {
        Install-SPSolution -Identity $WspName –GACDeployment -AllWebApplications
    }
    else
    {
        Install-SPSolution -Identity $wspName –GACDeployment -WebApplication $WebAppUrl
    }
}
WaitForSPSolutionJobToComplete $wspName
 
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

No comments:

Post a Comment