In this article, let us see how to make a generic script, which has the capability to activate the feature in three scopes. In the previous article, we saw only with the SiteCollection and WebApplication. I thought of making it a more generic and will be helpful across all the projects.
Now the CSV will be looks like below.
Based on the Scope column, the next column needs to be filled.
i.e., if the Scope is Site, then Give the SiteCollectionURL etc.,
Now, the script :-
$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
Happy Coding.
Sathish Nadarajan.
Leave a comment