# Write-Host -NoNewLine

cls

# Активация фичи SP 2010
function ActivateSPWebFeature($featureId) {
    if(!(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue)) #or -Site, -WebApplication, -Farm
    {
        Enable-SPFeature $featureId -Url $webUrl
        sleep -s 2
        if(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active!"
    }    
}
function ActivateSPSiteFeature($featureId) {
    if(!(Get-SPFeature $featureId -Site $siteUrl -ErrorAction:SilentlyContinue)) #or -Site, -WebApplication, -Farm
    {
        Enable-SPFeature $featureId -Url $siteUrl
        sleep -s 2
        if(Get-SPFeature $featureId -Site $siteUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active!"
    }    
}

# Деактивация фичи SP 2010
function DeactivateSPWebFeature($featureId) {
    if((Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue)) #or -Site, -WebApplication, -Farm
    {
        Disable-SPFeature $featureId -Url $webUrl -Confirm:$false
        sleep -s 2
        if(!(Get-SPFeature $featureId -Web $webUrl -ErrorAction:SilentlyContinue))
        {
            Write-Host "Feature " $featureId " deactivated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already unactive!"
    }    
}


# Проверяем наличае аргументов в командной строке 
if($args[0] -eq $null){
    Write-Host "Error! Empty argument 'webUrl'! Use like this 'FeaturesActivate.ps1 http://sps2010:101/MyTestNode'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}

Write-Host "Variables initialization ..."
$webUrl = $args[0]
$SPAdminTool=$env:CommonProgramFiles + "\Microsoft Shared\web server extensions\14\BIN\stsadm.exe"
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

# Грузим скриптлет SharePoint
Write-Host "SharePoint snapin loading ..."
if(!(Get-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction:SilentlyContinue))
{
    Add-PSSnapin Microsoft.SharePoint.Powershell
}

$w = Get-SPWeb $webUrl
$s = $w.Site

$siteUrl = $s.Url


# Активируем фичи
$FeatureName = "SPOKPR_WebParts"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPSiteFeature($FeatureName) 

$FeatureName = "SPOKPR_Lists"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_Nav"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_ListForms"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_WebInit"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_WebSec"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_WebPartInit"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

$FeatureName = "SPOKPR_DemoData"
Write-Host "Feature $FeatureName trying activate ..."
ActivateSPWebFeature($FeatureName) 

Write-Host "---------------------------------------------------------------"

# Выводим сообщение о завершении операций
$shell = New-Object -ComObject Wscript.Shell
$shell.popup("Операция завершена. Проверьте вывод консоли на наличае ошибок и затем нажмите OK.",0,"Результат" , 64)

#Write-Host " "
#Write-Host "Press any key ..."
#$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")