Using PowerShell To Automate WSP Deployments to SharePoint 2010

A couple handy Powershell functions I use in my deployment scripts:

 

#First load the SharePoint commands
add-PSSnapIn Microsoft.SharePoint.PowerShell
import-module WebAdministration

function WaitForJobToFinish([string]$SolutionFileName)
{ 
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job..."
    }
}

function DeployWebAppWSP($SolutionName, $DropLocation, $WebApplication)
{

        $SolutionPath = $DropLocation + "\" + $SolutionName
        
        Write-Host "Uninstall Old Solution: "$SolutionName
        Uninstall-SPSolution -identity $SolutionName -WebApplication $WebApplication -confirm:$false

        WaitForJobToFinish 

        Write-Host "Remove Old Solution: "$SolutionName
        Remove-SPSolution –identity $SolutionName -confirm:$false
 
        Write-Host "Add New Solution: "$SolutionName
        Add-SPSolution $SolutionPath
 
        Write-Host "Install solution to web application: "$WebApplication
        Install-SPSolution –identity $SolutionName -WebApplication $WebApplication -GACDeployment

        WaitForJobToFinish 

}

Leave a Reply