Created
March 21, 2016 14:40
-
-
Save rheid/8e89c85ac1a670964bd2 to your computer and use it in GitHub Desktop.
SharePoint Feature Activation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm | |
$LogFile = ".\FeatureActivation-$LogTime.rtf" | |
cls | |
##================================================================================================ | |
## Description : Feature Activation Demo - | |
## Author : Sathish Nadarajan | |
## Date : 23-Dec-2014 | |
##================================================================================================ | |
$Host.UI.RawUI.WindowTitle = "-- Feature Activation Demo --" | |
$StartDate = Get-Date | |
Write-Host -ForegroundColor White "------------------------------------" | |
Write-Host -ForegroundColor White "| Feature Activation Demo |" | |
Write-Host -ForegroundColor White "| Started on: $StartDate |" | |
Write-Host -ForegroundColor White "------------------------------------" | |
#start-transcript $logfile | |
$ErrorActionPreference = "Stop" | |
########################################################### Set the Exxecution Path ######################################### | |
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent | |
Set-Location $scriptBase | |
function AddPowerShellSnapin() | |
{ | |
try | |
{ | |
Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green | |
# Try to get the PowerShell Snappin. If not, then adding the PowerShell snappin on the Catch Block | |
Get-PSSnapin "Microsoft.SharePoint.PowerShell" | |
} | |
catch | |
{ | |
if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found")) | |
{ | |
Add-PSSnapin "Microsoft.SharePoint.PowerShell" | |
} | |
} | |
Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green | |
} | |
function ActivateFeatureInSiteCollectionScope($DisplayName, $siteurl) | |
{ | |
Write-Host "Activating Feature :- " $DisplayName " -: In Site Collection " $siteurl | |
$TempCount = (Get-SPSite $siteurl | %{ Get-SPFeature -Site $_ } | Where-Object {$_.DisplayName -eq $DisplayName} ).Count | |
if($TempCount -eq 0) | |
{ | |
# if not, Enable the Feature. | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $siteurl | |
} | |
else | |
{ | |
# If already Activated, then De-Activate and Activate Again. | |
Disable-SPFeature $DisplayName -Url $siteurl –Confirm:$false | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $siteurl | |
} | |
} | |
function ActivateFeatureInWebApplicationScope($DisplayName, $webApplicationUrl) | |
{ | |
Write-Host "Activating Feature :- " $DisplayName " -: In Web Application " $webApplicationUrl | |
$TempCount = (Get-SPWebApplication $webApplicationUrl | %{ Get-SPFeature -WebApplication $_ } | Where-Object {$_.DisplayName -eq $DisplayName} ).Count | |
if($TempCount -eq 0) | |
{ | |
# if not, Enable the Feature. | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $webApplicationUrl | |
} | |
else | |
{ | |
# If already Activated, then De-Activate and Activate Again. | |
Disable-SPFeature $DisplayName -Url $webApplicationUrl –Confirm:$false | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $webApplicationUrl | |
} | |
} | |
function ActivateFeatureInWebScope($DisplayName, $webUrl) | |
{ | |
Write-Host "Activating Feature :- " $DisplayName " -: In Sub Site " $webUrl | |
$TempCount = (Get-SPWeb $webUrl | %{ Get-SPFeature -Web $_ } | Where-Object {$_.DisplayName -eq $DisplayName} ).Count | |
if($TempCount -eq 0) | |
{ | |
# if not, Enable the Feature. | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $webUrl | |
} | |
else | |
{ | |
# If already Activated, then De-Activate and Activate Again. | |
Disable-SPFeature $DisplayName -Url $webUrl –Confirm:$false | |
Get-SPFeature $DisplayName | Enable-SPFeature -Url $webUrl | |
} | |
} | |
try | |
{ | |
AddPowerShellSnapin | |
$FeatureActivationDetailsCSV = $scriptBase + "\" + "FeatureActivationDetails.csv" | |
import-csv $FeatureActivationDetailsCSV | where { | |
if($_.Scope -eq "Site") | |
{ | |
ActivateFeatureInSiteCollectionScope $_.FeatureDisplayName $_.SiteCollectionURL | |
} | |
elseif($_.Scope -eq "WebApplication") | |
{ | |
ActivateFeatureInWebApplicationScope $_.FeatureDisplayName $_.WebApplicationURL | |
} | |
elseif($_.Scope -eq "Web") | |
{ | |
ActivateFeatureInWebScope $_.FeatureDisplayName $_.WebURL | |
} | |
} | |
Write-Host "Script Execution Completed Successfully" -ForegroundColor Green | |
} | |
catch | |
{ | |
Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red | |
} | |
#Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment