cls
# Проверяем наличае аргументов в командной строке их должно быть два: URL родительского узла и имя нового узла
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]

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

$webApp = Get-SPWebApplication ($webAppUrl+":"+$port) -ErrorAction:SilentlyContinue
if($webApp -ne $null)
{
    Write-Host "SharePoint web application deleting ..."    
    Remove-SPWebApplication ($webAppUrl+":"+$port) -DeleteIISSite -RemoveContentDatabases –Confirm:$False
    
    while($webApp -ne $null) # Ждем, пока не произойдет удаление
    {
        Write-Host "."                
        $webApp = Get-SPWebApplication ($webAppUrl+":"+$port) -ErrorAction:SilentlyContinue
        sleep -s 1
    }    
    Write-Host "---------------------------------------------------------------"
}

# Создаем веб-приложение
Write-Host "New SharePoint web application creating ..."
#Write-Host "---------------------------------------------------------------"
$webApp = New-SPWebApplication -Name ("WebApp"+$port) -Port $port -URL $webAppUrl -ApplicationPool ("AppPool"+$port) -ApplicationPoolAccount (Get-SPManagedAccount $owner) -ErrorAction:SilentlyContinue
Write-Host "---------------------------------------------------------------"

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

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