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

        $erpValid = Test-ERPName $ERP
        if (!$erpValid) {
            Write-Warning "$ERP is not a valid value"
            Write-ERPNames
            Write-Warning "Operation canceled"
            return
        }

        $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 }
        $needSecuritySet = $false
        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
            Write-Verbose "Security must be set on new tenant with Set-P1WebAuthentication command"
            $needSecuritySet = $true
        } 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"
        }

        Write-OK "Web instance created"

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

        $report = @{Port=$Port;NeedSecuritySet=$needSecuritySet}
        return $report
    }
}