Public/New-P1WebApp.ps1

function New-P1WebApp {
    <#
    .Synopsis
    Install a new PlannerOne web application.
 
    .Description
    Install a new PlannerOne web application in IIS.
 
    .Parameter Tenant
    The tenant name of this web application.
 
    .Parameter Port
    The port to be used by the new site.
 
    .Example
    # Install a PlannerOne web application for the tenant Prod on port 8042.
    New-P1WebApp -Tenant Prod -Port 8042
    #>

    [cmdletbinding()]
    param(
        [string] $Tenant,
        [int] $Port
    )
    Process
    {
    Write-Section "Creating new web instance..."
        $siteName = Get-SiteNameFromTenant $Tenant
        $poolName = Get-PoolNameFromTenant $Tenant
        $webAppName = Get-WebAppNameFromTenant $Tenant 
        $sitePath = "$IISRootPath\$siteName"
        $webAppPath = "$sitePath\$webAppName"
                
        $webBinPath = Get-PackageFolder($P1WebPackage)
        
        # Create Application Pool
        try {
            Get-WebAppPoolState $poolName | Out-Null
            Write-Verbose $poolName "Already Exists"
        } catch {
            # Assume it doesn't exist. Create it.
            New-WebAppPool -Name $poolName | Out-Null
        Write-Verbose "Pool $poolName created"
            Set-ItemProperty -Path IIS:\AppPools\$poolName -Name managedRuntimeVersion -Value v4.0
        }
        
        # Create Folder for the website
        if (!(Test-Path $sitePath)) {
            mkdir $sitePath | Out-Null
        } else {
            Remove-Item "$sitePath\*" -recurse -Force
        }
        
        # Link folder to repository
        New-Symlink $webAppPath "$webBinPath\WebServer"
        
        if ($Port -eq 0) {
            $Port = $DefaultWebPort
        }
        
    Write-Verbose "Choose free port in IIS from $Port"
        $Port = Get-FreeWebPort $Port
        Write-Verbose "Choosen port $Port"

        $site = Get-WebSite | Where-Object { $_.Name -eq $siteName }
        if ($null -eq $site) {
            Write-Verbose "Creating site: $siteName"            
            New-WebSite $siteName -PhysicalPath $sitePath -Port $Port | Out-Null
            
            Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath IIS:\ -location "$siteName"
            Set-WebConfigurationProperty -filter /system.webServer/security/authentication/anonymousAuthentication -name enabled -value false -PSPath IIS:\ -location "$siteName"            
            
            Write-Verbose "Creating web app: $webAppName"            
            New-WebApplication -Site $siteName -Name $webAppName -PhysicalPath $webAppPath -ApplicationPool $poolName | Out-Null
            
            Write-Verbose "Deploying custom cultures..."
            & "$webAppPath\Deploy\AddSubcultures.bat" > $null
        }

    $url = "http://localhost:$Port/$webAppName"
    Write-Output "Access PlannerOne at $url"
    Write-OK "Web created"
    }
}