Modules/businessdev.ALbuild.Containers/Private/Get-BcContainerArtifactInfo.ps1

function Get-BcContainerArtifactInfo {
    <#
    .SYNOPSIS
        Reads the BC version, country and artifact type a running container was built from.
 
    .DESCRIPTION
        There is no 'albuild.bcversion' label - the provenance labels record who created a container
        and how it is reachable, not what is inside it. What IS there is the artifact URL, which
        New-BcContainer leaves in the container's environment on purpose, and which carries all three
        facts in its path:
 
            .../sandbox/28.5.12345.0/de -> Sandbox, 28.5.12345.0, de
 
        Used by the capture manifest, where "which BC version do these pictures show" is the whole
        point of writing a manifest at all. Best-effort by design: a container built by something
        other than ALbuild may not carry the variable, and an incomplete manifest is better than a
        refused capture.
 
    .PARAMETER Name
        The container to read.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .OUTPUTS
        PSCustomObject with ArtifactUrl, Version, Country and ArtifactType; every field may be empty.
    #>

    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Name,
        [string] $DockerExecutable = 'docker'
    )

    $empty = [PSCustomObject]@{ ArtifactUrl = ''; Version = ''; Country = ''; ArtifactType = '' }

    $inspect = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru -Arguments @(
        'inspect', '-f', '{{range .Config.Env}}{{println .}}{{end}}', $Name
    )
    if (-not $inspect.Success) { return $empty }

    $artifactUrl = ''
    foreach ($line in ("$($inspect.StdOut)" -split "`r?`n")) {
        if ($line -match '^\s*artifactUrl=(.+?)\s*$') { $artifactUrl = $Matches[1]; break }
    }
    if (-not $artifactUrl) { return $empty }

    $parsed = [regex]::Match($artifactUrl, '/(sandbox|onprem)/([^/]+)/([^/?#]+)')
    if (-not $parsed.Success) {
        return [PSCustomObject]@{ ArtifactUrl = $artifactUrl; Version = ''; Country = ''; ArtifactType = '' }
    }

    [PSCustomObject]@{
        ArtifactUrl  = $artifactUrl
        Version      = $parsed.Groups[2].Value
        Country      = $parsed.Groups[3].Value
        ArtifactType = $(if ($parsed.Groups[1].Value -eq 'onprem') { 'OnPrem' } else { 'Sandbox' })
    }
}