Wednesday, 11 April 2012

Powershell restart features

Help function from http://sharepoint.stackexchange.com/questions/13051/powershell-script-to-check-feature-status-enabled-disabled-before-activating



function CheckSPFeatureActivated
{
param([string]$Id=$(throw "-Id parameter is required!"),
[Microsoft.SharePoint.SPFeatureScope]$Scope=$(throw "-Scope parameter is required!"),
[string]$Url)
if($Scope -ne "Farm" -and [string]::IsNullOrEmpty($Url))
{
throw "-Url parameter is required for scopes WebApplication,Site and Web"
}
$feature=$null

switch($Scope)
{
"Farm" { $feature=Get-SPFeature $Id -Farm }
"WebApplication" { $feature=Get-SPFeature $Id -WebApplication $Url }
"Site" { $feature=Get-SPFeature $Id -Site $Url –ErrorAction SilentlyContinue}
"Web" { $feature=Get-SPFeature $Id -Web $Url –ErrorAction SilentlyContinue}
}

#return if feature found or not (activated at scope) in the pipeline
$feature -ne $null
}


foreach($felement in $FEATURE_ARRAY)
{

$fActive = $FALSE
if($felement -eq "SpecialFeature")
{
$fActive = CheckSPFeatureActivated -Id $felement -Scope "Web" -Url $SUB_SITE_URL
}
else
{
$fActive = CheckSPFeatureActivated -Id $felement -Scope "Site" -Url $MAIN_URL
}

if($fActive)
{
Write-Host "Feature"$felement" is already activated. Restarting Feature." -foregroundcolor DarkCyan
if($felement -eq "SpecialFeature")
{

Write-Host "Deactivating...."
Disable-SPFeature -Identity $felement -url $SUB_SITE_URL -Confirm:$false
Write-Host "Activating..."
Enable-SPFeature -Identity $felement -url $SUB_SITE_URL

}
else
{
Write-Host "Deactivating...."
Disable-SPFeature -Identity $felement -url $MAIN_URL -Confirm:$false
Write-Host "Activating..."
Enable-SPFeature -Identity $felement -url $MAIN_URL
}
Write-Host $felement" Activated." -foregroundcolor Green
}
else
{
Write-Host $felement" not activated. " -foregroundcolor Yellow
if($felement -eq "SpecialFeature")
{
Enable-SPFeature -Identity $felement -url $SUB_SITE_URL
}
else
{
Enable-SPFeature -Identity $felement -url $MAIN_URL
}
Write-Host $felement" Activated." -foregroundcolor Green
}
}


No comments:

Post a Comment