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.
 
    .Example
    # Install a PlannerOne tenant named P1Prod.
    New-P1Tenant -Tenant P1Prod
    #>

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

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

        $package = Get-Package $P1WebPackage
        if ($package -eq $null) {    
            Write-Warning "Package $P1WebPackage 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 ($RemotingPort -eq 0) {
            $RemotingPort = 9191
        }
        
        if ($WebServicePort -eq 0) {
            $WebServicePort = 8731
        }

        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
        Write-OK "Web created"

        Write-Section "Registrating tenant..."
        Register-Tenant -Tenant $Tenant -RemotingPort $RemotingPort -WebServicePort $WebServicePort -WebAppName $WebAppName -SitePort $SitePort
        Write-OK "Registration done"

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

        Write-Warning "Run 'Set-P1Adapter' to configure ERP Adapter"
        Write-Warning "Run 'Start-P1Manager -Tenant $Tenant' to initialize your ERP environments"
        Write-OK "Tenant '$Tenant' created"
    }
}