Automatically Retract, Remove, Add and Deploy SharePoint 2010 WSP Solution Files with PowerShell

Posted: February 17, 2012 in Administration, PowerShell, SharePoint 2010

Deployment of SharePoint 2010 solutions could be a tiresome process especially if you have to do it often. In a recent project, I had to do weekly deployments of solutions throughout the development phase against our staging environment. This task was to be performed regularly and you could risk feeling like a robot typing the PowerShell commands in PowerShell. Why not try to automate such a mundane task?

Below I have included the PowerShell script I developed in order to automatically retract, uninstall, deploy and install SharePoint 2010 solution files. The PowerShell script is based on Reza Alirezaei’s script for installation of solutions from KWizCom. My script does not support upgrade of SharePoint 2010 solutions.

Basically, the script works in the following manner. First, it checks if there are any SharePoint 2010 solution files present in the current folder. If there is, it continues. For each of the solutions found, it checks whether they are deployed and installed in the current SharePoint 2010 farm. If they are found, they are retracted and removed from the farm. Last, the PowerShell script adds the solutions found to the farm, and then installs it.

To run the script, just type in the PowerShell console:

.\<SCRIPTNAME>.ps1

Just copy and paste the following section to a ps1-file:

function WaitForJobToFinish([string]$Identity)
{   
    $job = Get-SPTimerJob | ?{ $_.Name -like "*solution-deployment*$Identity*" }
    $maxwait = 30
    $currentwait = 0

    if (!$job)
    {
        Write-Host -f Red '[ERROR] Timer job not found'
    }
    else
    {
        $jobName = $job.Name
        Write-Host -NoNewLine "[WAIT] Waiting to finish job $jobName"        
        while (($currentwait -lt $maxwait))
        {
            Write-Host -f Green -NoNewLine .
            $currentwait = $currentwait + 1
            Start-Sleep -Seconds 2
            if (!(Get-SPTimerJob $jobName)){
                break;
            }
        }
        Write-Host  -f Green "...Done!"
    }
}

function RetractSolution([string]$Identity)
{
    Write-Host "[RETRACT] Uninstalling $Identity"    
    Write-Host -NoNewLine "[RETRACT] Does $Identity contain any web application-specific resources to deploy?"
    $solution = Get-SPSolution | where { $_.Name -match $Identity }
    if($solution.ContainsWebApplicationResource)
    {
        Write-Host  -f Yellow "...Yes!"        
        Write-Host -NoNewLine "[RETRACT] Uninstalling $Identity from all web applications"            
        Uninstall-SPSolution -identity $Identity  -allwebapplications -Confirm:$false
        Write-Host -f Green "...Done!"
    }
    else
    {
        Write-Host  -f Yellow  "...No!"        
        Uninstall-SPSolution -identity $Identity -Confirm:$false    
        Write-Host -f Green "...Done!"
    }

    WaitForJobToFinish

    Write-Host -NoNewLine  '[UNINSTALL] Removing solution:' $SolutionName
    Remove-SPSolution -Identity $Identity -Confirm:$false
    Write-Host -f Green "...Done!"
}

function DeploySolution([string]$Path, [string]$Identity)
{
    Write-Host -NoNewLine "[DEPLOY] Adding solution:" $Identity
    Add-SPSolution $Path
    Write-Host -f Green "...Done!"

    Write-Host -NoNewLine "[DEPLOY] Does $Identity contain any web application-specific resources to deploy?"
    $solution = Get-SPSolution | where { $_.Name -match $Identity }

    if($solution.ContainsWebApplicationResource)
    {
        Write-Host -f Yellow "...Yes!"        
        Write-Host -NoNewLine "[DEPLOY] Installing $Identity for all web applications"    
        Install-SPSolution -Identity $Identity -AllWebApplications -GACDeployment

    }
    else
    {
        Write-Host -f Yellow "...No!"        
        Write-Host -NoNewLine "[DEPLOY] Globally deploying $Identity"    
        Install-SPSolution -Identity $Identity -GACDeployment
    }
    Write-Host -f Green "...Done!"

    WaitForJobToFinish
}

$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
    Write-Host "[INIT] Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

Write-Host "[INIT] Locating WSP files to be deployed"
$wsps = Get-ChildItem . *.wsp | where-object { !($_.psiscontainer) }

foreach ($wsp in $wsps)
{
    $identity = $wsp.Name
    $path = $wsp.FullName
    Write-Host "[INFO] ----------------------------------------"
    Write-Host "[INFO] Installing $Identity"
    Write-Host -NoNewLine "[INFO] Determining if $Identity is already installed"

    $isInstalled = Get-SPSolution | where { $_.Name -eq $identity }
    if ($isInstalled)
    {
        Write-Host -ForegroundColor Yellow "...Yes!"
        (RetractSolution $identity)
        (DeploySolution $path $identity)
    }
    else
    {
        Write-Host -ForegroundColor Yellow "...No!"
        (DeploySolution $path $identity)
    }

    Write-Host -NoNewline "[INFO] Installation and deployment of $Identity"
    Write-Host -ForegroundColor Green "...Done!"
}
About these ads
Comments
  1. This is really attention-grabbing, You’re a very skilled blogger. I’ve joined your rss feed and look forward to in quest of extra of your fantastic post. Additionally, I have shared your website in my social networks!

  2. daniel says:

    script was very helpful thanks

  3. […] or you can write a script for install/update/remote automatically the package files. Check this post. […]

  4. chaitanya says:

    Brilliant …………IT WAS ACTUAL SCRIPT I WAS HUNTING FOR……..Thank u very Much ….Keep Rocking With Posts

  5. Thiru says:

    Excellent post … thanks.

  6. Anita says:

    Wonderful! You saved me so much time. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s