Monday, August 5, 2013

SharePoint PowerShell Deactivate feature sub site level

Here is the peace of Powershell code for that


[System.Reflection.Assembly]::LoadWithPartialName("System.Xml")


function Feature_Deactivation_subsites($featName)
{
    try
    {
   
        [xml]$userfile = Get-Content C:\Site.xml
        Write-Log "Feature_Deactivation_subsites" "Process started.."
       
        foreach($site in $userfile.Sites.Site)
        {   
            $url = $site.Url
            $spSite=Get-SPWeb $url
            DeactivateFeature $spSite $featName
      
            foreach($web in $spSite.Webs)
            {   
                DeactivateFeature $web $featName
                 Write-Log "Feature_Deactivation_subsites" "Feature deactivated in web "$web.Url
            }         
           
        }
        Write-Log "Feature_Deactivation_sitecollection" "Operation completed successfully.."
        write-host -foregroundcolor green "Operation completed succesfully.."
       
    }
    catch [System.Exception]
    {
        write-Host -foregroundcolor red "Exception: "$_.Exception
    }
}
function DeactivateFeature($web, $featName)
{
        $Featuretobeactivated = Get-SPFeature -Web $web.Url | Where {$_.DisplayName -eq $featName}
        $webURL = $web.Url
                   
        if($Featuretobeactivated -eq $null)
        {
           
            Write-Host "Feature not found "$webURL
            Write-Log "Feature_Deactivation_subsites" "Feature not active in $webURL"
        }
        else
        {
            Disable-SPFeature -Identity $featName -Url $web.Url –Confirm:$false
            Write-Log "Feature_Deactivation_subsites" "Feature deactivated in web "$webURL
            Write-host -foregroundcolor yellow "Feature deactivated at web "$webURL
        }
}

function Write-Log($FunctionName,$message)
{       
    $now = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
    $now + "`t$message" >>  C:\$FunctionName.txt
}

Thank you !!


No comments:

Post a Comment