# Write-Host -NoNewLine

# Удаляем корректно решение
function RemoveSolution($SolutionName) {

    $solution = Get-SPSolution $SolutionName -ErrorAction:SilentlyContinue
    if($solution)
    {    
        if($solution.Deployed –eq $true)
        {
            #Write-Host "Please, wait a bit ..."
            Uninstall-SPSolution $solution -Confirm:$false  -AllWebApplications 
            $secs = 0
            while($solution.Deployed –eq $true) # Ждем, пока не произойдет uninstall
            {
                #Write-Host "."                
                Write-Progress -Activity "Working..." -CurrentOperation "$secs s. waiting" -Status "Please wait."
                sleep -s 1
                $secs = $secs + 1
            }
        }
        sleep -s 10
        Remove-SPSolution $solution -Confirm:$false;            
    }
}

# Добавляем решение
function AddInstallSolution($SolutionName) {

    Write-Host "$SolutionName processing ..."

    if(Get-SPSolution -Identity $SolutionName -ErrorAction:SilentlyContinue) 
    {   
        Write-Host "Old solution $SolutionName removing ..."
        RemoveSolution($SolutionName) # Удаляем решение, если оно уже есть на ферме
    }
    
    $FullPathToSolution = $executingScriptDirectory+"\" + $SolutionName
    Write-Host "New solution $FullPathToSolution adding ..."
    $solution = Add-SPSolution -LiteralPath "$FullPathToSolution"     
    
    Write-Host "Solution $SolutionName installing..."
    Install-SPSolution $solution -GACDeployment -Force -WebApplication $siteUrl
    while($solution.Deployed –eq $false)
    {
        Write-Progress -Activity "Working..." -CurrentOperation "$secs s. waiting" -Status "Please wait."
        sleep -s 1
        $secs = $secs + 1
    }
}

cls
# Проверяем наличае аргументов в командной строке их должно быть два: URL узла и URL коллекции узлов
if($args[0] -eq $null){
    Write-Host "Error! Empty argument 'webUrl'! Use like this 'setup.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
Write-Host $siteUrl

# Place for Write-Progress block
Write-Host " "
Write-Host " "
Write-Host " "
Write-Host " "
Write-Host " "
Write-Host " "

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

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

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