Monday, November 22, 2010

Power Shell .wsp deployment scripts for SharePoint 2010

Copy the PowerShell scriptlet below and paste it in a notepad, modify your Site Collection URL in the code marked in yellow below and save it with a .ps1 extension.

Also have a look at Powershell script to deploy multiple solutions (.wsp) to your site collection. See the full post here: http://www.sharepointfix.com/2011/07/powershell-script-to-deploy-multiple.html
=======================================================================
Add-PsSnapin Microsoft.SharePoint.PowerShell

#Site Collection URL - Give your site collection url in quotation marks
$SiteUrl="http://localhost"

#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#solutions to deploy
$SolutionName="PrintListItem.wsp"
$SolutionPath = Join-Path (Get-ScriptDirectory) $SolutionName
echo "Extracting information from $SolutionPath"

$SolutionPath= $SolutionPath
$FeatureName="PrintListItem_Feature1"

#Feature name
$FeatureID= $(Get-SPFeature -limit all | ? {($_.displayname -eq $FeatureName)}).Id
$SingleSiteCollection = Get-SPSite $SiteUrl

#Admin service
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}

Write-Host 'DEACTIVATING FEATURE ...'

Disable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
Write-Host 'FEATURE HAS BEEN DEACTIVATED SUCCESSFULLY.'

#Uninstall
Write-Host 'UNINSTALLING SOLUTION ...'

$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 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'

Write-Host 'REMOVING SOLUTION ...'

if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
    Remove-SPSolution $SolutionName -Confirm:$false

Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'
}

Write-Host 'ADDING SOLUTION ...'

Add-SPSolution $SolutionPath  | Out-Null

Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'

Write-Host 'DEPLOYING SOLUTION ...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}

#use '-force' paramater to install all commands in this if statement

if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Force -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Force -Confirm:$false
}

while ($Solution.Deployed -eq $false)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'

Write-Host 'ACTIVATING FEATURE ...'

if ($FeatureName -ne $null)
{
    Enable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
}
#message
Write-Host 'FEATURE HAS BEEN ACTIVATED SUCCESSFULLY.'


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

Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish

=====================================================================

Now create a new notepad file and copy the code mentioned below, and then save the below mentioned code in a .bat file to automate the PowerShell deployment.

cd /d %~dp0
powershell -noexit -file ".PrintListItemDeploymentScript.ps1" "%CD%"
pause

2 comments:

  1. Hi Mehuil,

    I like to have script for Install, Deploy, Activate & Retract multiple wsp files on single web application...


    Thanks,
    Sree

    ReplyDelete
  2. Hi Thanjai,

    I have written a new post on your request, please find it here: http://www.sharepointfix.com/2011/07/powershell-script-to-deploy-multiple.html

    ReplyDelete