Public/Register-P1Tenant.ps1

function Register-P1Tenant {
    <#
    .Synopsis
    DEV TOOL - Use New-P1Tenant cmdlet if you want to install a new PlannerOne tenant.
    Register a new PlannerOne tenant.

    .Description
    DEV TOOL.
    Register a new PlannerOne tenant with the given parameters.
    Does not deploy the product.

    Every name are set by convention from tenant name if they are not overloaded.
    Keep convention name if possible for easier maintenance.
    Web service port and remoting port are declined from web port.
    Use BinPath to force main binary path (NavAdapterApplication.exe, PlannerOneService.exe, PlannerOne.exe...)

    .Parameter Tenant
    The tenant name.

    .Parameter BinPath
    The binary path of main PlannerOne service / debug process.

    .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.
    Register-P1Tenant -Tenant NavDev -WebPort 8142 -ERP NAV -BinPath "C:\dotNet\Projects\PlannerOne\branches\dev\NavAdapter\bin\Debug"
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [string] $BinPath,
        [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
        }

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

        Write-Section "Registering tenant $Tenant..."
        if ($WebPort -eq 0) {
            $WebPort = $DefaultWebPort
        }

        if (!$WebAppName) {
            $WebAppName = Get-WebAppNameFromTenant $Tenant
            Write-Verbose "Web application name: $WebAppName"
        }

        if ($WebPort -eq 0) {
            $WebPort = 8042
            Write-Verbose "Web port set to default: $WebPort"
        }

        if ($WebServicePort -eq 0) {
            $WebServicePort = $WebPort + 1
            Write-Verbose "Web service port: $WebServicePort"
        }

        if ($RemotingPort -eq 0) {
            Write-Verbose "Remoting port: $RemotingPort"
            $RemotingPort = $WebPort + 2
        }

        $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 $WebPort -Adapter $adapter -BinPath $BinPath
        Restore-P1WebSecurity -Tenant $Tenant
        Write-OK "Registration done"

        Get-P1tenant $Tenant
    }
}