New-ALOpsExternalDeployer.ps1

function New-ALOpsExternalDeployer(){
    [CmdletBinding()]
    param
    (
        [string] $ServerInstance = "BC",
        [string] $ALOpsExternalDeployerEndpoint = "http://127.0.0.1:7040/",
        [string] $CustomScriptPath
    )
       
    Import-ALOpsNavManagementDlls -ServerInstance $ServerInstance

    Write-Host "*** Linking ServerInstance [$($ServerInstance)] to ALOps External Deployer [$($ALOpsExternalDeployerEndpoint)]"
    Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName "ExternalNavAppDeploymentEndpoint" -KeyValue $ALOpsExternalDeployerEndpoint

    $ModuleVersionMajor = (Get-Module -Name "Microsoft.Dynamics.Nav.Management").Version.Major
    if ($ModuleVersionMajor -ge 21){
        Write-Host "*** BC Version >= v21. Using Certificate Authentication"

        $AzureTenantID = "0d9730f5-6cb6-475e-a9c9-88e78a3d8ab1"
        $AzureClientID = "f0b08e80-18c1-419d-8cfb-952f6e56f14c"
        $AccessToken   = "AppId=$($AzureClientID);CertificateSubjectName=BCDeployer;TenantId=$($AzureTenantID);CertificateStoreLocation=LocalMachine"

        Set-NAVServerConfiguration -ServerInstance $ServerInstance `
                                   -KeyName AccessTokenConnectionString `
                                   -KeyValue "$($AccessToken)"

        Set-NAVServerConfiguration -ServerInstance $ServerInstance `
                                   -KeyName ExtensionProxyServiceClientId `
                                   -KeyValue "$($AzureClientID)"


    } else {
        Write-Host "*** BC Version =< v20. Using API-Key Authentication"

        $AppDeployDetails = @{ 
            "ScriptFolder" = "$($CustomScriptPath)"; 
            "ContainerId" = ""; 
            "ServerInstance" = "$($ServerInstance)"; 
            "Tenant" = "default"
        }

        Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName LocalServiceApiKey -KeyValue ( $AppDeployDetails | ConvertTo-Json -Depth 100)
    }

    if ((Get-NAVServerInstance -ServerInstance $ServerInstance).state -ilike "Running"){
        Restart-NAVServerInstance -ServerInstance $ServerInstance
    }
}