function DeactivateWebFeatureByWebUrlAndFeatureId($webUrl, $featureId) {	
    if((Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue))
    {
        Disable-SPFeature $featureId -Url $webUrl -Confirm:$false
        sleep -s 2
        if(!(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue))
        {
            Write-Host "Feature " $featureId " deactivated successfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already unactive!" -ForegroundColor Yellow
    }    
}

function DeactivateSiteFeatureByWebUrlAndFeatureId($webUrl, $featureId) {	
    if((Get-SPFeature $featureId -Site $webUrl -ErrorAction:SilentlyContinue))
    {
        Disable-SPFeature $featureId -Url $webUrl -Confirm:$false
        sleep -s 2
        if(!(Get-SPFeature $featureId -Site $webUrl -ErrorAction:SilentlyContinue))
        {
            Write-Host "Feature " $featureId " deactivated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already unactive!" -ForegroundColor Yellow
    }    
}

function DeactivateWebApplicationFeatureByWebUrlAndFeatureId($webAppUrl, $featureId) {	
    if((Get-SPFeature $featureId -WebApplication $webAppUrl -ErrorAction:SilentlyContinue))
    {
        Disable-SPFeature $featureId -Url $webAppUrl -Confirm:$false
        sleep -s 2
        if(!(Get-SPFeature $featureId -WebApplication $webAppUrl -ErrorAction:SilentlyContinue))
        {
            Write-Host "Feature " $featureId " deactivated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already unactive!" -ForegroundColor Yellow
    }    
}


function ActivateWebApplicationFeatureByWebUrlAndFeatureId($webAppUrl, $featureId) {
	Write-Host "Feature $featureId trying activate ..."
    if(!(Get-SPFeature $featureId -WebApplication $webAppUrl -ErrorAction:SilentlyContinue))
    {
        Enable-SPFeature $featureId -Url $webAppUrl
		sleep -s 2
        if(Get-SPFeature $featureId -WebApplication $webAppUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated successfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active! Try reactivate ..." -ForegroundColor Yellow
		DeactivateWebApplicationFeatureByWebUrlAndFeatureId $webAppUrl $featureId
		ActivateWebApplicationFeatureByWebUrlAndFeatureId $webAppUrl $featureId
    }  
}


function ActivateWebFeatureByWebUrlAndFeatureId($webUrl, $featureId) {
	Write-Host "Feature $featureId trying activate ..."
    if(!(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue))
    {
        Enable-SPFeature $featureId -Url $webUrl -ErrorAction: Stop
		sleep -s 2
        if(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated successfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active! Try reactivate ..." -ForegroundColor Yellow
		DeactivateWebFeatureByWebUrlAndFeatureId $webUrl $featureId
		ActivateWebFeatureByWebUrlAndFeatureId $webUrl $featureId
    }  
}

function ActivateSiteFeatureBySiteUrlAndFeatureId($siteUrl, $featureId) {
	Write-Host "Feature $featureId trying activate ..."
    if(!(Get-SPFeature $featureId -Site $siteUrl -ErrorAction:SilentlyContinue)) 
    {
        Enable-SPFeature -force $featureId -Url $siteUrl -ErrorAction: Stop
        sleep -s 2
        if(Get-SPFeature $featureId -Site $siteUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated successfully"
        }
    }
    else
    {
		Write-Host "Feature " $featureId "already active! Try reactivate ..." -ForegroundColor Yellow
		DeactivateSiteFeatureByWebUrlAndFeatureId $siteUrl $featureId
		ActivateSiteFeatureBySiteUrlAndFeatureId $siteUrl $featureId
    }    
}