Public/New-P1Tenant.ps1

function New-P1Tenant {
    <#
    .Synopsis
    Install a new PlannerOne tenant (service + web app).
 
    .Description
    Install a new PlannerOne tenant with the given parameters.
    One tenant should be:
    * One Windows service
    * One application pool
    * One web site
    * One web application
    Every name are set by convention from tenant name.
    You can force web application name for backward compatibility.
    Keep convention name if possible for easier maintenance.
 
    .Parameter Tenant
    The tenant name.
 
    .Parameter WebServicePort
    The port to be used by AppServer for REST API.
 
    .Parameter RemotingPort
    The port to be used by AppServer for Remoting API.
 
    .Parameter WebPort
    The customized port to be used by WebServer site.
 
    .Parameter WebAppName
    The customized name to use for web application.
 
    .Parameter ERP
    The ERP to use (NAV, AP, SAGE).
 
    .Example
    # Install a PlannerOne tenant named P1Prod.
    New-P1Tenant -Tenant P1Prod -ERP SAGE
    #>

    [cmdletbinding()]
    param( 
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [int] $RemotingPort,
        [int] $WebServicePort,
        [int] $WebPort,
        [string] $WebAppName,
        [Parameter(Mandatory=$true)]
        [string] $ERP
    )
    Process
    {
        if (Test-Tenant $Tenant) {
            Write-Warning "Tenant $Tenant already exist. Creation canceled."
            Write-Warning "Type Get-Help New-P1Tenant for more information."
            return
        }

        if ($ERP -eq "") {
            Write-Warning "ERP is not defined."
            Write-Warning "Please select call New-P1Tenant again with -ERP flag (NAV, AP, SAGE)"
            return
        }

        $packageName = Get-PackageName $P1SrvPackage $ERP
        $package = Get-Package $packageName
        if ($package -eq $null) {    
            Write-Warning "Package $packageName does not exist."
            Write-Warning "Call Install-P1Package with missing packages."
            return
        }

        $packageName = Get-PackageName $P1WebPackage $ERP
        $package = Get-Package $packageName
        if ($package -eq $null) {    
            Write-Warning "Package $packageName does not exist."
            Write-Warning "Call Install-P1Package with missing packages."
            return
        }

        $iisCommand = Get-Command New-WebAppPool
        if ($iisCommand -eq $null) {
            Write-Warning "Command New-WebAppPool does not exist."
            Write-Warning "Please run Test-P1Prerequisites."
            return
        }

        Write-Section "Creating tenant..."
        if ($WebPort -eq 0) {
            $WebPort = $DefaultWebPort
        }
        
        if (!$WebAppName) {
            $WebAppName = Get-WebAppNameFromTenant $Tenant
        }


        # create web app before registering to get the good web port
        $SitePort = New-P1WebApp -Tenant $Tenant -Port $WebPort -WebAppName $WebAppName -ERP $ERP
        Write-OK "Web created on port $SitePort"

        if ($WebServicePort -eq 0) {
            $WebServicePort = $SitePort + 1
        }

        if ($RemotingPort -eq 0) {
            $RemotingPort = $SitePort + 2
        }

        # TODO: test that these 2 last ports are free
        $adapter = $ERPToAdapter.Get_Item($ERP)
        Write-Verbose "Adapter associated to ERP is $adapter"
        Write-Section "Registrating tenant..."
        Register-Tenant -Tenant $Tenant -RemotingPort $RemotingPort -WebServicePort $WebServicePort -WebAppName $WebAppName -SitePort $SitePort -Adapter $adapter
        Write-OK "Registration done"

        New-P1ServerInstance -Tenant $Tenant -ERP $ERP
        Write-OK "Windows service created"

        Write-Warning "Run 'Set-P1Adapter' to configure Adapter"
        Write-OK "Tenant '$Tenant' created"
    }
}