cls
# Проверяем наличае аргументов в командной строке их должно быть четыре
if($args[0] -eq $null){
    Write-Host "Error! Empty argument 'Port'! Use like this 'NewSite.ps1 89 http://sps2010 sdc\portalservices sdc\aborovtsov'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}
if($args[1] -eq $null){
    Write-Host "Error! Empty argument 'WebAppUrl'! Use like this 'NewSite.ps1 89 http://sps2010 sdc\portalservices sdc\aborovtsov'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}
if($args[2] -eq $null){
    Write-Host "Error! Empty argument 'Owner'! Use like this 'NewSite.ps1 89 http://sps2010 sdc\portalservices sdc\aborovtsov'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}
if($args[3] -eq $null){
    Write-Host "Error! Empty argument 'Secondary owner'! Use like this 'NewSite.ps1 89 http://sps2010 sdc\portalservices sdc\aborovtsov'"
    Write-Host " "
    Write-Host "Press any key ..."
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")    
    return
}


Write-Host "Variables initialization ..."
$port = $args[0]
$webAppUrl = $args[1]
$owner = $args[2]
$secOwner = $args[3]
$url = $webAppUrl+":"+$port

$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 "---------------------------------------------------------------"


$site = Get-SPSite $url -ErrorAction:SilentlyContinue
if($site -ne $null)
{
    # Удаляем коллекцию
    Write-Host "SharePoint site collection deleting ..."
    Remove-SPSite –Identity $url –Confirm:$False
    Write-Host "---------------------------------------------------------------"
}

# Создаем коллекцию сайтов
Write-Host "New SharePoint site collection creating ..."
New-SPSite $url -OwnerAlias $owner -SecondaryOwnerAlias $secOwner -HostHeaderWebApplication $webApp -Name ("SiteColl"+$port) -Template "STS#1" -Language 1049
Write-Host "---------------------------------------------------------------"

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