# Добавляем решение
function AddInstallSolution($SolutionName) {           
    
    Write-Host "Solution $SolutionName installing..."
    $FullPathToSolution = $executingScriptDirectory+"\" + $SolutionName
    $solution = Add-SPSolution -LiteralPath "$FullPathToSolution"    
    if ($solution.ContainsWebApplicationResource –eq $false) 
    {
      Install-SPSolution $solution -GACDeployment
    } 
    else
    {
      Install-SPSolution $solution -GACDeployment -WebApplication $webAppUrl
    }    
   
    while($solution.Deployed –eq $false)
    {
        Write-Progress -Activity "Working..." -CurrentOperation "$secs s. waiting" -Status "Please wait."
        sleep -s 1
        $secs = $secs + 1
    }
}

function ActivateSPWebFeature($featureId) {
    if(!(Get-SPFeature $featureId -Web $webAppUrl -ErrorAction:SilentlyContinue)) #or -Site, -WebApplication, -Farm
    {
        Enable-SPFeature $featureId -Url $webAppUrl
        if(Get-SPFeature $featureId -Web $webAppUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active!"
    }    
}

function ActivateSPSiteFeature($featureId) {
    if(!(Get-SPFeature $featureId -Site $webAppUrl -ErrorAction:SilentlyContinue)) #or -Site, -WebApplication, -Farm
    {
        Enable-SPFeature $featureId -Url $webAppUrl
        if(Get-SPFeature $featureId -Site $webAppUrl -ErrorAction:SilentlyContinue)
        {
            Write-Host "Feature " $featureId " activated sucsesfully"
        }
    }
    else
    {
        Write-Host "Feature " $featureId "already active!"
    }    
}

cls
# Проверяем наличие аргументов в командной строке их должно быть два: URL сайта и URL коллекции сайтов
if($args[0] -eq $null){
    Write-Host "Error! Empty argument 'siteUrl'! Use like this 'CollaborationSetup.ps1 http://myportal:90'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}

Write-Host "Variables initialization ..."
$webAppUrl = $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
}


# Устанавливаем нестрогий контроль файлов у Веб-приложения
Write-Host "Web application BrowserFileHandling configuring ..."
$webapp = Get-SPWebApplication $webAppUrl
$webapp.BrowserFileHandling = “Permissive” # “Strict”
$webapp.update()

# Устанавливаем решение
Write-Host "SharePoint solutions processing ..."
Write-Host "---------------------------------------------------------------"
AddInstallSolution("IntegrationDelo.wsp")
Write-Host "---------------------------------------------------------------"

# Активируем возможности
$FeatureId = "{8f40ece7-640b-46a0-9394-5bd9ed475a1c}"
Write-Host "Feature 'IntegrationDeloJob' trying activate ..."
ActivateSPSiteFeature($FeatureId) # Интеграция с СЭД. Создание задания

$FeatureId = "{28e9fbf3-5cdf-4705-be9d-0edacdbf5dfa}"
Write-Host "Feature 'IntegrationDeloRootSite' trying activate ..."
ActivateSPWebFeature($FeatureId) # Интеграция с СЭД (корневой сайт)

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

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