Public/Set-P1Adapter.ps1

function Set-P1Adapter {
    <#
    .Synopsis
    Configuration of the Microsoft Dynamics NAV Adapter.
 
    .Description
    Configure the Microsoft Dynamics NAV Adapter for PlannerOne.
 
    .Parameter Tenant
    The tenant name.
 
    .Parameter ERP
    The ERP name (NAV, REST, AP)
 
    .Parameter Server
    The ERP host server name.
     
    .Parameter SOAPPort
    The ERP Web service SOAP port.
     
    .Parameter AuthenticationMode
    The ERP authentication mode (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 NAVVersion
    The NAVVersion to use for Powershell module tools (60, 70, 80, 90...)
                                                             
    .Example
    # Configure a NAV adapter
    Set-P1NavConfiguration -Tenant nodb -ServiceDomain ORTEMS -ServiceLogin PlannerOne -ServicePassword Planner1 -Server locahost -InstanceName DynamicsNAV90 -SOAPPort 9047
    #>

    [cmdletbinding()]
    param(      
        [Parameter(Mandatory=$true)]
        [string] $Tenant,
        [Parameter(Mandatory=$true)]
        [string] $ERP,
        [string] $Server,
        [int] $SOAPPort,
        [string] $AuthenticationMode,
        [bool] $SSL,
        [int] $ClientPort,
        [Parameter(Mandatory=$true)]
        [string] $InstanceName,
        [string] $ERPTenantName,
        [string] $ServiceDomain,
        [Parameter(Mandatory=$true)]
        [string] $ServiceLogin,
        [Parameter(Mandatory=$true)]
        [string] $ServicePassword,
        [switch] $AutoPort,
        [string] $NAVVersion
    )
    Process
    {
        if (!(Test-Tenant $Tenant)) {
            Write-Warning "Tenant $Tenant does not exist."
            Write-Warning "Operation canceled."
            return
        }
        
        if ($ERP -eq "") {
            Write-Warning "ERP is not defined"
            return      
        }
       
        if ($ERP -ne "NAV" -and $ERP -ne "REST") {
            Write-Warning "ERP $ERP not recognized"
            return
        }
        
        if ($ERP -eq "NAV") {
            Write-Section "Configuring NAV Adapter..."
                
            if ($InstanceName -eq "") {
                Write-Warning "Microsoft Dynamics NAV Instance name is missing."
                return        
            }       
                        
            $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 ($ERP -eq "REST") {
            Write-Section "Configuring REST Adapter..."
            if ($AuthenticationMode -eq "") {
                $AuthenticationMode = "Basic"
            }

            Set-RESTParameters $Tenant $Server $SOAPPort $AuthenticationMode $SSL $InstanceName $ServiceDomain $ServiceLogin $ServicePassword $ERPTenantName
        }
                
        $serviceName = Get-ServiceNameFromTenant $Tenant
        Write-Output "Restarting PlannerOne service $serviceName..."
        Restart-Service -Name $serviceName
        Write-OK "Configuration done"
    }
}