In this article, let us see, how to Activate the Site Scoped, WebApplication Scoped Features using PowerShell in SharePoint 2013.
$ErrorActionPreference = "Stop"
cls
############################# Feature Names ###############################
$SiteScopedFeature = "MyFeature.InternalName" #Target Site Collection
$WebApplicationScopedFeature = "MyWebApplicationFeature.InternalName" #Target Web Application
######################################################################################
$args = new-object string[] 1
$args[0] = "CloudShare"
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 ActivateFeature($DisplayName, $siteurl)
{
Write-Host "Activating " $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, $siteurl)
{
Write-Host "Activating " $DisplayName "In Web Application " $siteurl
$TempCount = (Get-SPWebApplication $siteurl | %{ Get-SPFeature -WebApplication $_ } | 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
}
}
try
{
AddPowerShellSnapin
#Read arguement and store environment URL
if($args[0] -eq 'Cloud')
{
$SiteCollectionURL ="http://SathishServer:1001/sites/Test"
$WebApplicationURL ="http://SathishServer:1001/"
}
elseif($args[0] -eq 'DEV')
{
# Give the URLs appropriately
}
elseif ($args[0] -eq 'INT')
{
# Give the URLs appropriately
}
elseif ($args[0] -eq 'QA')
{
# Give the URLs appropriately
}
elseif ($args[0] -eq 'PROD')
{
# Give the URLs appropriately
}
else
{
Write-Host -f Yellow "Environment not specified - please pass appropriate environment to deploy (Possible values are DEV INT QA PROD)"
Log-Message $logFilePath "Environment not specified - please pass appropriate environment to deploy (Possible values are DEV INT QA PROD)"
exit
}
ActivateFeature $SiteScopedFeature $SiteCollectionURL
ActivateFeatureInWebApplicationScope $WebApplicationScopedFeature $WebApplicationURL
Write-Host "Script Execution Completed Successfully" -ForegroundColor Green
}
catch
{
Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red
}
The script will actually De-Activate and Activate the Feature specified on the top of the script. The script is mostly self explanatory. Hence, I am not explaining much about that.
Happy Coding.
Sathish Nadarajan.
Leave a comment