Modules/businessdev.ALbuild.Containers/Public/Get-BcContainerLabel.ps1

function Get-BcContainerLabel {
    <#
    .SYNOPSIS
        Reads a Docker label off a container - the authoritative answer to "who created this?".
 
    .DESCRIPTION
        New-BcContainer stamps provenance onto every container it creates ('albuild.managed',
        'albuild.createdBy', 'albuild.createdAt', plus 'albuild.protocol' and 'albuild.reachability').
        This is the reader for it.
 
        Why a label and not a state file: the MCP server tracked its containers in pool.json under
        LOCALAPPDATA and the CLI in ~/.albuild/containers.json. Both are process- and profile-local, so
        the answer to "is this mine?" changed with the agent id, a second server process, a different
        user, a different host, or simply the other backend - and an agent was denied the removal of a
        container it had created seconds earlier. The container carries its own provenance now, and
        every caller that can reach Docker can read it.
 
        Best-effort by design: an unknown container, an unset label or a Docker hiccup all yield an
        empty string rather than an error, so a caller can fall back instead of failing. Distinguish
        "no label" from "no container" with Get-BcContainer if that matters.
 
    .PARAMETER Name
        Container name.
 
    .PARAMETER Label
        The label key to read, e.g. 'albuild.managed'.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .OUTPUTS
        System.String - the label value, or '' when the container or the label does not exist.
 
    .EXAMPLE
        Get-BcContainerLabel -Name bld -Label albuild.managed
        Returns 'true' for a container ALbuild created.
 
    .EXAMPLE
        if ((Get-BcContainerLabel -Name $c -Label albuild.managed) -eq 'true') { Remove-BcContainer -Name $c }
        Remove without asking for approval only what ALbuild created.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('ContainerName')]
        [string] $Name,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string] $Label,

        [string] $DockerExecutable = 'docker'
    )

    process {
        # SuccessExitCodes 0,1: 'docker inspect' exits 1 for a container that does not exist, which is an
        # answer ("not managed"), not a failure to report.
        $inspect = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru -SuccessExitCodes @(0, 1) `
            -Arguments @('inspect', '-f', ('{{index .Config.Labels "' + $Label + '"}}'), $Name)

        if (-not $inspect.Success) { return '' }

        # Go's text/template prints '<no value>' for a missing map key; that is "unset", not a value.
        $value = "$($inspect.StdOut)".Trim()
        if ($value -eq '<no value>') { return '' }
        return $value
    }
}