Modules/businessdev.ALbuild.Containers/Private/ConvertFrom-BcContainerList.ps1

function ConvertFrom-BcContainerList {
    <#
    .SYNOPSIS
        Parses 'docker ps --format {{json .}}' output into container objects.
    .DESCRIPTION
        Internal, pure helper (no I/O). Docker emits one JSON object per line; each is parsed and
        projected onto a stable shape (Name, Status, Image, Id, Running, Labels, Managed, CreatedBy).
 
        Labels come along because 'docker ps' already returns them - so a listing can say who created
        a container without a second 'docker inspect' per row. Note that docker renders them as one
        flat 'k=v,k2=v2' string, which is ambiguous for a value containing a comma; none of ALbuild's
        own labels do. Where the answer has to be authoritative (the removal gate), read the single
        label with Get-BcContainerLabel instead - a listing is a hint, inspect is the authority.
    #>

    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param(
        [Parameter(Mandatory)]
        [AllowEmptyString()]
        [AllowNull()]
        [string] $Output
    )

    if ([string]::IsNullOrWhiteSpace($Output)) { return @() }

    $list = foreach ($line in ($Output -split "`r?`n")) {
        if ([string]::IsNullOrWhiteSpace($line)) { continue }
        try { $obj = $line | ConvertFrom-Json } catch { continue }
        $status = [string]$obj.Status

        # Guarded lookup: an older docker (or a changed --format) may not carry Labels at all, and under
        # Set-StrictMode a missing property is a terminating error rather than $null.
        $labels = @{}
        $raw = if ($obj.PSObject.Properties['Labels']) { [string]$obj.Labels } else { '' }
        foreach ($pair in ($raw -split ',')) {
            if ([string]::IsNullOrWhiteSpace($pair)) { continue }
            $split = $pair.IndexOf('=')
            if ($split -lt 1) { continue }
            $labels[$pair.Substring(0, $split)] = $pair.Substring($split + 1)
        }

        [PSCustomObject]@{
            Name      = [string]$obj.Names
            Status    = $status
            Image     = [string]$obj.Image
            Id        = [string]$obj.ID
            Running   = ($status -like 'Up*')
            Labels    = $labels
            Managed   = ($labels['albuild.managed'] -eq 'true')
            CreatedBy = if ($labels.ContainsKey('albuild.createdBy')) { $labels['albuild.createdBy'] } else { '' }
        }
    }
    return @($list)
}