Public/Set-P1Adapter.ps1

function Set-P1Adapter {
    <#
    .Synopsis
    Configuration of the adapter for the ERP.
 
    .Description
    Configure the adapter for PlannerOne.
 
    .Parameter Tenant
    The tenant name.
 
    .Parameter Server
    The ERP host server name.
 
    .Parameter SOAPPort
    The ERP Web service SOAP port.
 
    .Parameter RESTPort
    The ERP Web service SOAP port.
 
    .Parameter AuthenticationMode
    The ERP authentication mode (Certificate, Negotiate, NTLM, Digest, Basic).
 
    .Parameter SSL
    Does the ERP server use SSL.
 
    .Parameter ClientPort
    The port for external client software (RTC...)
 
    .Parameter InstanceName
    The ERP server instance name.
 
    .Parameter ERPTenantName
    The ERP tenant name.
 
    .Parameter ServiceDomain
    The PlannerOne technical account domain.
 
    .Parameter ServiceLogin
    The PlannerOne technical account login.
 
    .Parameter ServicePassword
    The PlannerOne technical account password.
 
    .Parameter AutoPort
    Read ports from ERP configuration provided with InstanceName
 
    .Parameter ExtraParameter
    Extra parameter specific for ERP.
    For NAV, to specify NAVVersion of Powershell module tools to use: -ExtraParameter "NAVVersion=90"
 
    .Example
    # Configure an adapter
    Set-P1Adapter -Tenant nodb -ServiceDomain ORTEMS -ServiceLogin PlannerOne -ServicePassword Planner1 -Server locahost -InstanceName ERPInstance -SOAPPort 9047
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [string] $Server,
        [int] $SOAPPort,
        [int] $RESTPort,
        [string] $AuthenticationMode,
        [bool] $SSL,
        [int] $ClientPort,
        [Parameter(Mandatory=$true)]
        [string] $InstanceName,
        [string] $ERPTenantName,
        [string] $ServiceDomain,
        [string] $ServiceLogin,
        [string] $ServicePassword,
        [switch] $AutoPort,
        [string] $ExtraParameter
    )
    Process
    {
        if (!(Test-Tenant $Tenant)) {
            Write-Warning "Tenant $Tenant does not exist."
            Write-Warning "Operation canceled."
            return
        }

        $tenantInfo = Get-TenantInfo $Tenant
        if ($tenantInfo -eq $null) {
                Write-Warning "No tenant information available"
                Write-Warning "Configuration cancel"
                return
        }

        $Adapter = $tenantInfo.Adapter

        if ($Adapter -eq "" -or $Adapter -eq $null) {
            Write-Warning "Adapter is not defined"
            return
        }

        if ($Adapter -ne "NAV" -and $Adapter -ne "REST") {
            Write-Warning "Adapter $Adapter not recognized"
            return
        }


        if ($AuthenticationMode -eq "Certificate") {
            if ($Adapter -ne "REST") {
                Write-Warning "Certificate authentication is only available for REST adapter."
                return
            }

            $SSL = $true
        }

        if ($Adapter -eq "NAV") {
            Write-Section "Configuring NAV Adapter..."

            if ($InstanceName -eq "") {
                Write-Warning "Microsoft Dynamics NAV Instance name is missing."
                return
            }

            [string] $NAVVersion = ""
            if ($ExtraParameter -ne "") {
                Write-Verbose "Parsing ExtraParameter $ExtraParameter"
                $splited = $ExtraParameter.Split("=")
                if ($splited[0] -eq "NAVVersion" -and $splited.Length -ge 2) {
                    $NAVVersion = $splited[1]
                    Write-Verbose "NAVVersion is set to $NAVVersion"
                }
            }

            $NAVConfig = Get-NAVParametersFromConfig -InstanceName $InstanceName -NAVVersion $NAVVersion
            if ($AutoPort) {
                if ($NAVConfig -eq $null) {
                    Write-Warning "No NAV configuration. PlannerOne Configuration canceled."
                    return
                }

                $InstanceName = $NAVConfig.InstanceName
                $SOAPPort = $NAVConfig.SOAPPort
                $ClientPort = $NAVConfig.ClientPort
            }

            if ($NAVConfig -ne $null) {
                if ($NAVConfig.SOAPPort -ne $SOAPPort) {
                    $port = $NAVConfig.SOAPPort
                    Write-Warning "SOAP Port $SOAPPort is not the one set for Microsoft Dynamics NAV instance $InstanceName`: $port"
                    Write-Warning "You can use -AutoPort switch to automatically use the ports configured in Microsoft Dynamics NAV."
                    Write-Warning "PlannerOne adapter configuration canceled."
                    return;
                }

                if ($NAVConfig.SOAPServicesEnabled -ne "true") {
                    Write-Warning "SOAP service is not enabled in Microsoft Dynamics NAV instance $InstanceName"
                    Write-Warning "PlannerOne adapter configuration canceled."
                    return;
                }

                if ($NAVConfig.ServicesDefaultTimeZone -ne "Server Time Zone") {
                    $timezone = $NAVConfig.ServicesDefaultTimeZone
                    Write-Warning "SOAP service time zone should be 'Server Time Zone'. Current value in Microsoft Dynamics NAV instance $InstanceName`: $timezone"
                    Write-Warning "PlannerOne adapter configuration canceled."
                    return;
                }
            } else {
                Write-Warning "Cannot read Microsoft Dynamics NAV configuration: parameters won't be validated. Run with -verbose for more technical informations."
            }

            Set-NAVParameters $Tenant $Server $SOAPPort $AuthenticationMode $SSL $ClientPort $InstanceName $ServiceDomain $ServiceLogin $ServicePassword $ERPTenantName
        }

        if ($Adapter -eq "REST") {
            Write-Section "Configuring REST Adapter..."
            if ($AuthenticationMode -eq "") {
                $AuthenticationMode = "Basic"
            }

            if ($RESTPort -eq 0 -and $SOAPPort -ne 0) {
                $RESTPort = $SOAPPort
            }

            Set-RESTParameters $Tenant $Server $RESTPort $AuthenticationMode $SSL $InstanceName $ServiceDomain $ServiceLogin $ServicePassword $ERPTenantName
        }

        $serviceName = Get-ServiceNameFromTenant $Tenant
        Write-Output "Restarting PlannerOne service $serviceName..."

        Stop-Service -Name $ServiceName -Force

        Write-Output "Waiting 5 seconds for service to really stop."
        Start-Sleep -seconds 5

        Start-Service -Name $serviceName
        Write-OK "Configuration done"
    }
}