Navigation

Thursday, 26 February 2015

Add SharePoint farm solution and activate feature using PowerShell

There are two parts to this PowerShell script – the first part is fairly straightforward as it simply adds and installs a solution file into the farm. I have added a line to the end of the script to wait until the solution has fully deployed before continuing:

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity http://portal

#Add solution file to the farm
$solution = Add-SPSolution -LiteralPath "C:\Install\Solution.wsp"

#Deploy solution
#Add -WebApplication $webApp to the line below if solution contains Web Application scoped resources
Install-SPSolution $solution -Force -GACDeployment

#Wait for solution to be deployed
do {Start-Sleep -s 1} while ($solution.Deployed -eq $false)

If you have multiple solution files to deploy, then it is worth checking out this script from Gary Lapointe, which enables you to install and deploy solution files in bulk using an XML configuration file.

The second part of the script activates a site or site collection feature, which is also pretty standard, except that I have provided four scenarios:

Activate site scoped feature in one site collection
$site = Get-SPSite http://portal
Enable-SPFeature -Identity "FeatureName" -Url $site.Url
$site.Dispose()

Activate site scoped feature for all site collections in a Web Application
$webApp = Get-SPWebApplication -Identity http://portal
$webApp | Get-SPSite -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}

Activate web scoped feature in one site
$web = Get-SPWeb http://portal
Enable-SPFeature -Identity "FeatureName" -Url $web.Url
$web.Dispose()

Activate web scoped feature for all sites in a site collection
$site = Get-SPSite http://portal
$site | Get-SPWeb -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}
$site.Dispose()

No comments:

Post a Comment