3

Please can you give me the best practice PowerShell code to:-

A. Add, install and enable feature at

-Web

-Site Collection

-Web Application

-Farm scope

B. Deactivate, Uninstall and Remove feature at

-Web

-Site Collection

-Web Application

-Farm scope

1 Answer 1

4
function Deactivate-SPFeature
{
    param ($FeatureID, $SiteUrl, $WebApplicationUrl)
    $Feature = Get-SPFeature -Identity $FeatureID -ErrorAction SilentlyContinue
    $IsActiveFeature
    If ($Feature -eq $null)
    {
        Write-Warning "The specified feature ($FeatureID) was not found."
        return
    }
    If ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Farm)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Farm -ErrorAction SilentlyContinue
        If ($IsActiveFeature -eq $null)
        {
            Write-Warning "The specified feature ($FeatureID) is not activated on the Farm."
            return;
        }
        Write-Host "Deactivating Feature $FeatureID."
        Disable-SPFeature -Identity $FeatureID  -confirm:$false
        Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
        return;
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::WebApplication)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -WebApplication $WebApplicationUrl -ErrorAction SilentlyContinue
        If ($IsActiveFeature -eq $null)
        {
            Write-Warning "The specified feature ($FeatureID) is not activated in the Web application ($WebApplicationUrl)."
            return;
        }
        Write-Host "Deactivating Feature $FeatureID."
        Disable-SPFeature -Identity $FeatureID -Url $WebApplicationUrl -confirm:$false
        Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
        return;
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Site $SiteUrl -ErrorAction SilentlyContinue
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Web)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Web $SiteUrl -ErrorAction SilentlyContinue
    }
    If ($IsActiveFeature -eq $null)
    {
        Write-Warning "The feature $FeatureID is not activated on the site $SiteUrl"
        return;
    }

    Write-Host "Deactivating Feature $FeatureID."
    Disable-SPFeature -Identity $FeatureID  -Url $SiteUrl -confirm:$false
    Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
}

function EnableOrSkip-SPFeature
{
    param ($FeatureID, $SiteUrl)
    $Feature = Get-SPFeature -Identity $FeatureID -ErrorAction SilentlyContinue
    If ($Feature -eq $null)
    {
        Write-Warning "The specified feature ($FeatureID) was not found."
        return
    }
    If ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Farm)
    {
        $Feature = Get-SPFeature -Identity $FeatureID -Farm -ErrorAction SilentlyContinue
        if ($Feature -ne $null)
        {
            Write-Host "Feature  $FeatureID is already activated on the Farm"  -foregroundcolor Green
            return;
        }
        Write-Host "Activating feature $FeatureID"
        Enable-SPFeature -Identity $FeatureID -Confirm:$false 
        Write-Host "Feature $FeatureID activated successfully" -foregroundcolor Green
        return;
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::WebApplication)
    {
        $Feature = Get-SPFeature -Identity $FeatureID -WebApplication $SiteUrl -ErrorAction SilentlyContinue
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site)
    {
        $Feature = Get-SPFeature -Identity $FeatureID -Site $SiteUrl -ErrorAction SilentlyContinue
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Web)
    {
        $Feature = Get-SPFeature -Identity $FeatureID -Web $SiteUrl -ErrorAction SilentlyContinue
    } 
    If ($Feature -ne $null)
    {
        Write-Host "Feature  $FeatureID is already activated on the site $SiteUrl" -foregroundcolor Green
        return;
    }
    Write-Host "Activating feature $FeatureID"
    Enable-SPFeature -Identity $FeatureID -Url $SiteUrl  -Confirm:$false 
    Write-Host "Feature $FeatureID activated successfully" -foregroundcolor Green
}

function AddAndDeployGlobally-SPSolution
{
    param ( $SolutionPackageName, $SolutionPath)
    # Check if solution exists.
    $Solution = Get-SPSolution -Identity $SolutionPackageName -ErrorAction SilentlyContinue
    if ($Solution -ne $null)
    {
        RetractAndRemove-SPSolution $SolutionPackageName
    }
    # Add solution
    Write-Host "Adding solution $SolutionPackageName."
    Add-SPSolution -LiteralPath $SolutionPath
    Write-Host "$SolutionPackageName added successfully."  -foregroundcolor Green
    # Deploy solution
    $Solution = Get-SPSolution | ? {($_.Name -eq $SolutionPackageName) -and ($_.Deployed -eq $false)}
    if($Solution -ne $null)
    {
        # Install solution on Farm
        Write-Host "Deploying solution to Farm."
        if($Solution.ContainsWebApplicationResource)   
        {
            Install-SPSolution -Identity $SolutionPackageName -WebApplication $WebApplication -       GACDeployment -Confirm:$false
        }
        else
        {
            Install-SPSolution -Identity $SolutionPackageName -GACDeployment -Confirm:$false
        }
        Restart-SPAdminV4
        while ($Solution.Deployed -eq $false)
        {
            Write-Host -NoNewLine .
            Start-Sleep 2
        }
        Write-Host "$SolutionPackageName deployed successfully to Farm." -foregroundcolor Green   
    }
}

function RetractAndRemove-SPSolution
{
    param ( $SolutionPackageName )
    # Check if solution exists.
    $Solution = Get-SPSolution | ? {($_.Name -eq $SolutionPackageName) -and ($_.Deployed -eq $true)}  -ErrorAction SilentlyContinue
    if ($Solution -ne $null)   
    {   
        Write-Host "Rectracting solution: $SolutionPackageName" 
        if($Solution.ContainsWebApplicationResource)   
        {   
            Uninstall-SPSolution $SolutionPackageName -AllWebApplications -Confirm:$false   
        }   
        else   
        {   
            Uninstall-SPSolution $SolutionPackageName -Confirm:$false   
        }
        Restart-SPAdminV4
        while ($Solution.JobExists)
        {
            Write-Host -NoNewLine .
            Start-Sleep 2
        }
        Write-Host "$SolutionPackageName retracted successfully."  -foregroundcolor Green
        if ($(Get-SPSolution | ? {$_.Name -eq $SolutionPackageName}).Deployed -eq $false)
        {
            Write-Host "Removing solution: $SolutionPackageName" 
            Remove-SPSolution $SolutionPackageName -Confirm:$false
            Write-Host "$SolutionPackageName removed successfully."  -foregroundcolor Green
        }
    } 
}

function Restart-SPAdminV4
{
    Stop-Service SPAdminV4
    Start-SPAdminJob 
    Start-Service SPAdminV4
}

Source: http://sharepointnadeem.blogspot.com/2011/08/sharepoint-2010-powershell-deployment.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.