Friday, January 15, 2010

SharePoint 2010 - Solution Deployment using PowerShell

Couple posts ago I described how to deploy a SharePoint solution using STSADM tool. But there is another way to deploy your solution and do any customization. You can do same operations as retract/deploy, add/delete solution etc. using PowerShell. Actually I recommend using PowerShell because it works faster and more flexible. You can dynamically activate or deactivate features on all webs in your site collection, you can dynamically apply any changes to webs etc.

To get started with PowerShell, run the SharePoint 2010 Management Console located in your Microsoft SharePoint 2010 Products folder in your start menu. This automatically loads the Microsoft.SharePoint.PowerShell snappin, so that we can execute SharePoint commands.

Below I will describe how to do common deployment steps using STSADM and PowerShell commands:

  1. Adding solution.
    stsadm –o addsolution –name SolutionPackage.wsp
    
    Same operation in powershell:
    Add-SPSolution c:\code\SolutionPackage.wsp
    
    Only one difference, If you would like use relative path, you must add “./” before solution name.

  2. Deploying solution.
    stsadm –o deploysolution –name SolutionPackage.wsp –url http://servername –allowgacdeployment –immediate
    stsadm -o execadmsvcjobs
    
    We must also follow this operation with a call to stsadm with the execadmsvcjobs operation.
    Install-SPSolution SolutionPackage.wsp –WebApplication http://servername –GACDeployment
    
    Executing this command does the deployment operation. You don’t have to fire off something to execute a job later like you did with STSADM. You can deploy solution to specified web application (as shown above) or to all web applications using –AllWebApplications parameter.

  3. Uninstalling solution.
    stsadm -o retractsolution -name SolutionPackage.wsp -immediate
    stsadm -o execadmsvcjobs
    
    Uninstall-SPSolution SolutionPackage.wsp –WebApplication http://servername
    
  4. Removing solution.
    And lastly removing solution.
    stsadm -o deletesolution -name SolutionPackage.wsp
    
    Remove-SPSolution SolutionPackage.wsp

  5. Activating feature.
    stsadm -o activatefeature -name FeatureName -url http://siteurl
    
    You can also manipulate with features using PowerShell.
    Enable-SPFeature FeatureName -Url http://siteurl
    
  6. Deactivating feature.
    stsadm -o deactivatefeature -name FeatureName -url http://siteurl
    
    Disable-SPFeature FeatureName -Url SiteUrl
    

The nice thing is that you can script these things together very easily and create highly flexible PowerShell scripts. I use the following script for deploying the solution and activating the feature:

$SiteUrl = $args[0]
$SolutionFolder = $args[1]
$SolutionName = $args[2]
$FeatureName = $args[3]

$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

Add-PSSnapin microsoft.sharepoint.powershell

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName       
}

$SingleSiteCollection = Get-SPSite $SiteUrl

Write-Host

if ($FeatureName -ne $null)
{
    Write-Host "Deactivating feature: $FeatureName" -NoNewline
        $WebAppsFeatureId = $(Get-SPFeature -limit all | ? {($_.displayname -eq $FeatureName)}).Id
        if (($WebAppsFeatureId -ne $null) -and ($SingleSiteCollection.Features | ? {$_.DefinitionId -eq $WebAppsFeatureId}))
        {
            Disable-SPFeature $FeatureName -Url $SiteUrl -Confirm:$false
        }
    Write-Host " - Done."
}

Write-Host "Rectracting solution: $SolutionName" -NoNewline
    $Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}
    if ($Solution -ne $null)
    {
        if($Solution.ContainsWebApplicationResource)
        {
            Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
        }
        else
        {
            Uninstall-SPSolution $SolutionName -Confirm:$false
        }
    }
    
    while ($Solution.JobExists)
    {
        Start-Sleep 2
    }
Write-Host " - Done."
    
Write-Host "Removing solution: $SolutionName" -NoNewline
    if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
    {
        Remove-SPSolution $SolutionName -Confirm:$false
    } 
Write-Host " - Done."
Write-Host

Write-Host "Adding solution: $SolutionName" -NoNewline
    $SolutionPath = $SolutionFolder + $SolutionName
    Add-SPSolution $SolutionPath | Out-Null
Write-Host " - Done."
    
Write-Host "Deploying solution: $SolutionName" -NoNewline
    $Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}
    if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
    {
        Install-SPSolution $SolutionName -AllWebApplications -GACDeployment -Confirm:$false
    }
    else
    {
        Install-SPSolution $SolutionName -GACDeployment -Confirm:$false
    }
    while ($Solution.Deployed -eq $false)
    {
        Start-Sleep 2
    }
Write-Host " - Done."
    
if ($FeatureName -ne $null)
{
    Write-Host "Activate feature: $FeatureName" -NoNewline
        Enable-SPFeature $FeatureName -Url $SiteUrl
    Write-Host " - Done."
}

if (-not $IsAdminServiceWasRunning)
{
    Stop-Service $AdminServiceName     
}

Echo Finish

Save that script to deploy.ps1. Now you can use it in command file. Here is the example:

@echo off
Set DeploymentPackageFolder="D:\Projects\DemandManagement\trunk\Src\Programs\Programs\DeploymentPackage\"

Set SiteUrl="http://localhost/pwa"
Set SolutionFolder="./"
Set SolutionName="Programs.wsp"
Set FeatureName="Programs_Programs"

cd %DeploymentPackageFolder%

PowerShell -file .\Deploy.ps1 %SiteUrl% %SolutionFolder% %SolutionName% %FeatureName%

Note: You must run that command file under x64 shell, otherwise “microsoft.sharepoint.powershell” snapin can’t be added.

I hope this post will help you to start with PowerShell.

6 comments:

  1. Thanks! Your script is helpful. Only one things I'd like to add: such construction doesn't work for me (SP2010)

    while ($Solution.JobExists)
    {
    Start-Sleep 2
    }

    I rewrote it:

    while ($Solution.JobExists)
    {
    Start-Sleep 2

    $Solution = Get-SPSolution -Identity $SolutionName -ErrorAction SilentlyContinue
    }

    ReplyDelete
  2. I was impressed with this blog, although the material is simple but the results are extraordinary

    cara menggugurkan kandungan
    obat aborsi
    kalkulator kehamilan

    ReplyDelete