Modules/businessdev.ALbuild.Apps/Public/Install-Bc365App.ps1

function Install-Bc365App {
    <#
    .SYNOPSIS
        Installs one or more 365 business development apps into a Business Central container.
 
    .DESCRIPTION
        Installs 365 business development dependency apps (by app id) into a build container using the
        365 business development app installer. The installer script is downloaded inside the container
        and run with the requested app ids; it resolves and installs each app for the container's BC
        version. Requires Windows + a running Docker engine. Native replacement for the V1 Install365App
        task.
 
    .PARAMETER Name
        Container name.
 
    .PARAMETER AppId
        One or more 365 app ids to install.
 
    .PARAMETER InstallerUrl
        URL of the installer script. Defaults to the ALBUILD_365APP_INSTALLER_URL environment variable,
        or the public 365 business development installer.
 
    .PARAMETER DockerExecutable
        The Docker executable to use. Default 'docker'.
 
    .EXAMPLE
        Install-Bc365App -Name bld -AppId '0f94d4ef-5c3a-4002-93f2-2a2be05219c0'
 
    .OUTPUTS
        PSCustomObject: Container, AppId, InstallerUrl.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [Alias('ContainerName')] [string] $Name,
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string[]] $AppId,
        [string] $InstallerUrl,
        [string] $DockerExecutable = 'docker'
    )

    if (-not $InstallerUrl) {
        $InstallerUrl = if ($env:ALBUILD_365APP_INSTALLER_URL) {
            $env:ALBUILD_365APP_INSTALLER_URL
        }
        else {
            'https://downloads.365businessdev.com/assets/Install-Apps.ps1'
        }
    }

    if (-not $PSCmdlet.ShouldProcess($Name, "Install 365 app(s): $($AppId -join ', ')")) {
        return [PSCustomObject]@{ Container = $Name; AppId = $AppId; InstallerUrl = $InstallerUrl }
    }

    $output = Invoke-BcContainerCommand -ContainerName $Name -DockerExecutable $DockerExecutable -Variables @{
        InstallerUrl = $InstallerUrl
        AppIds       = $AppId
    } -ScriptBlock {
        $local = Join-Path $env:TEMP 'Install-365Apps.ps1'
        Invoke-WebRequest -Uri $InstallerUrl -OutFile $local -UseBasicParsing -ErrorAction Stop
        & $local -apps $AppIds
        Write-Output "Installed 365 app(s): $($AppIds -join ', ')"
    }
    Write-ALbuildLog -Level Success "Installed 365 app(s) into '$Name': $($AppId -join ', ').`n$($output.Trim())"

    return [PSCustomObject]@{ Container = $Name; AppId = $AppId; InstallerUrl = $InstallerUrl }
}