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.
 
    .Parameter WebAppName
    The name to use for web application.
 
    .Parameter LegacySite
    The legacy site name. Use only for migration.
 
    .Parameter ERP
    The ERP to use (NAV, AP, ERP)
 
    .Example
    # Install a PlannerOne web application for the tenant Prod on port 8042.
    New-P1WebApp -Tenant Prod -Port 8042 -ERP SAGE
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [int] $Port,
        [string] $WebAppName,
        [string] $LegacySite,
        [Parameter(Mandatory=$true)]
        [string] $ERP
    )
    Process
    {
        Write-Verbose "Creating new web instance..."

        if ($LegacySite -eq "") {
            $siteName = Get-SiteNameFromTenant $Tenant
        } else {
            Write-Verbose "Site name forced to $LegacySite"
            $siteName = $LegacySite
        }

        $poolName = Get-PoolNameFromTenant $Tenant

        if (!$WebAppName) {
            $WebAppName = Get-WebAppNameFromTenant $Tenant
        }

        $sitePath = "$IISRootPath\$siteName"
        $webAppPath = "$sitePath\$WebAppName"
        
        $packageName = Get-PackageName $P1WebPackage $ERP
        $webBinPath = Get-PackageFolder($packageName)
        Write-Verbose "Web package folder is $webBinPath"
    
        # 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"
    
        $site = Get-WebSite | Where-Object { $_.Name -eq $siteName }
        if ($null -eq $site) {
            if ($Port -eq 0) {
                $Port = $DefaultWebPort
            }
        
            Write-Verbose "Choose free port in IIS from $Port"
            $Port = Get-FreeWebPort $Port
            Write-Verbose "Choosen port $Port"
            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"            
        } else {
            Write-Verbose "Site $siteName already exist. Using existing site."
        }

        $webApp = Get-WebApplication -Name $WebAppName -Site $siteName

        if ($null -eq $webApp) {
            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
        } else {
            Write-Verbose "Web application $WebAppName already exist"
        }

        $url = "http://localhost:$Port/$WebAppName"
        Write-Verbose "Access PlannerOne at $url"

        return $Port
    }
}