DimeScheduler.BCContainer.ps1

<#PSScriptInfo
.VERSION 0.0.6.0
.GUID d700bd9c-42aa-41cc-8b3d-b3f2bb1e2a9d
.AUTHOR Hendrik Bulens
.ICONURI https://cdn.dimescheduler.com/dime-scheduler/v2/shape.png
#>


#Requires -Module BcContainerHelper

<#
.SYNOPSIS
Gets and deploys Business Central in a Docker container.
It's merely a wrapper around the BcContainerHelper module (https://github.com/microsoft/navcontainerhelper) that's ready to go.
 
.DESCRIPTION
Downloads the image from the registry and deploys it to a Docker container.
This is done using the NavContainerHelper PowerShell module.
 
.PARAMETER isNav
Use this switch to use a NAV container
 
.PARAMETER version
See this blog for more information: https://freddysblog.com/2020/06/25/working-with-artifacts/
 
.PARAMETER name
Name of the new container (if the container already exists it will be replaced)
 
.PARAMETER useShortcuts
Use this switch to create desktop shortcuts
 
.PARAMETER includeTestToolkit
Use this switch too include the test toolkit
 
.PARAMETER memoryLimit
Memory limit for the container (default is unlimited for Windows Server host else 4G)
 
.PARAMETER dns
Use this parameter to override the default dns settings in the container (corresponds to --dns on docker run)
 
.PARAMETER sharedDrive
The path of the shared drive where to drop the license file
 
.PARAMETER licensefile
The path of the license file. If it's a NAV container, it needs to be a web URL.
See this link for more instructions: https://freddysblog.com/2018/03/20/navcontainerhelper-license/
 
.PARAMETER user
The admin user name
 
.PARAMETER password
The admin password
#>


param(   
    [Parameter(mandatory = $false, HelpMessage = "The BC/NAV version")]
    [String]$version = '24.0',
    
    [Parameter(mandatory = $false, HelpMessage = "The name of the container")]
    [String]$name = 'BC24',

    [Parameter(mandatory = $false, HelpMessage = "Set switch to use NAV")]
    [switch]$isNav,
       
    [Parameter(mandatory = $false, HelpMessage = "The local drive to share with the Docker container")]
    [String]$sharedDrive = 'C:\docker\DS',

    [Parameter(mandatory = $false, HelpMessage = "The BC license file")]
    [String]$licensefile = 'c:\docker\DS\BC.bclicense',

    [Parameter(mandatory = $false, HelpMessage = "The BC admin")]
    [String]$user = 'admin',

    [Parameter(mandatory = $false, HelpMessage = "The BC admin password")]
    [String]$password = 'admin',

    [Parameter(mandatory = $false, HelpMessage = "Switch to include shortcuts")]
    [Switch]$useShortcuts = $true,

    [Parameter(mandatory = $false, HelpMessage = "Switch to include test toolkit")]
    [Switch]$includeTestToolkit = $false,
    
    [Parameter(mandatory = $false, HelpMessage = "Memory limit for the container (default is unlimited for Windows Server host else 4G-8G should be enough)")]
    [string]$memoryLimit = "8G",
    
    [Parameter(mandatory = $false, HelpMessage = "Use this parameter to override the default dns settings in the container (corresponds to --dns on docker run)")]
    [string]$dns = '8.8.8.8'
)

Write-Output ""
Write-Output ""
Write-Output "*"
Write-Output "**"
Write-Output "***"
Write-Output "****"
Write-Output "*****"
Write-Output "******"
Write-Output "*******"
Write-Output "********"
Write-Output "*********"
Write-Output "**********"
Write-Output "***********"
Write-Output "************"
Write-Output ""
Write-Output "_____ _ _____ _ _ _"
Write-Output "| __ \(_) / ____| | | | | | |"
Write-Output "| | | |_ _ __ ___ ___ | (___ ___| |__ ___ __| |_ _| | ___ _ __"
Write-Output "| | | | | '_ ` _ \ / _ \ \___ \ / __| '_ \ / _ \/ _` | | | | |/ _ \ '__|"
Write-Output "| |__| | | | | | | | __/_ ____) | (__| | | | __/ (_| | |_| | | __/ |"
Write-Output "|_____/|_|_| |_| |_|\___(_)_____/ \___|_| |_|\___|\__,_|\__,_|_|\___|_|"
Write-Output ""
Write-Output ""
Write-Output "Creating Docker container $name v$version."
Write-Output ""
Write-Output "************"
Write-Output "***********"
Write-Output "**********"
Write-Output "*********"
Write-Output "********"
Write-Output "*******"
Write-Output "******"
Write-Output "*****"
Write-Output "****"
Write-Output "***"
Write-Output "**"
Write-Output "*"
Write-Output ""
Write-Output ""

if ($isNav) {    
    $confirmation = Read-Host "NAV containers are in early preview mode. Want to continue?"
    if ($confirmation -ne 'y') {
        return;
    }
}

if (-not (Get-Module BcContainerHelper -ListAvailable)) {
    Install-Module BcContainerHelper -Scope CurrentUser -Force
}

$appbacpacuri = ''
$tenantbacpacuri = ''

# Shared drive: map the parameter (by default, that is 'C:\docker\DS') to the local C:\dsfiles directory on Docker
$additionalParameters = @("-v" + $sharedDrive + ":c:\dsfiles")
if ($appbacpacuri -ne '' -and $tenantbacpacuri -ne '') {
    $additionalParameters += @("--env appbacpac=""$appbacpacuri""", "--env tenantBacpac=""$tenantbacpacuri""")
}

# Build credentials
$pw = ConvertTo-SecureString -String $password -AsPlainText -Force
$credential = New-Object PSCredential $user, $pw

# Fetch the URL of the requested container
$artifactUrl = If ($isNav -eq $false) { Get-BCArtifactUrl -type OnPrem -version $version -country w1 } Else { Get-NavArtifactUrl -nav $version -country w1 }
Write-Output "Artifact URL for $version : $artifactUrl"

# Value for shortcuts: None, Desktop or StartMenu
$shortCuts = 'None';
if ($useShortcuts.IsPresent) {
    $shortCuts = 'Desktop';
}

$containerCreationParameter = @{
    accept_eula              = $true
    containerName            = $name
    auth                     = 'UserPassword'
    Credential               = $credential
    alwaysPull               = $true
    doNotExportObjectsToText = $true
    usessl                   = $false
    updateHosts              = $true
    assignPremiumPlan        = $true
    shortcuts                = $shortcuts
    artifactUrl              = $artifactUrl
    additionalParameters     = $additionalParameters
    ACCEPT_OUTDATED          = $true
    licenseFile              = If ($isNav) { $null } Else { $licensefile }
    includeCSide             = If ($isNav) { $true } Else { $false }
    includeTestToolkit       = $includeTestToolkit.IsPresent
    includetestlibrariesonly = $true
    memoryLimit              = $memoryLimit
    dns                      = $dns
}

try {
    New-BcContainer @containerCreationParameter -ErrorAction Stop
}
catch {
    Write-Host "Couldn't create container"
    Write-Host $_
    return $_
}