Modules/businessdev.ALbuild.Environments/Public/Get-BcEnvironment.ps1

function Get-BcEnvironment {
    <#
    .SYNOPSIS
        Lists ALbuild-provisioned Business Central environments (containers).
 
    .DESCRIPTION
        Returns the containers tagged as ALbuild environments, with their metadata labels (type,
        owner, public URL, expiry) and whether they have expired.
 
    .PARAMETER Name
        Optional exact environment name filter.
 
    .PARAMETER DockerExecutable
        The Docker executable to use (default 'docker').
 
    .OUTPUTS
        PSCustomObject with Name, Type, Owner, Url, ExpiresAt, Expired, Running.
    #>

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

    $list = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru `
        -Arguments @('ps', '--all', '--no-trunc', '--filter', 'label=albuild.environment', '--format', '{{json .}}')
    if (-not $list.Success) { throw "Failed to list environments: $($list.StdErr.Trim())" }

    foreach ($line in ($list.StdOut -split "`r?`n")) {
        if ([string]::IsNullOrWhiteSpace($line)) { continue }
        try { $container = $line | ConvertFrom-Json } catch { continue }
        $containerName = [string]$container.Names
        if ($Name -and $containerName -ne $Name) { continue }

        $inspect = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru `
            -Arguments @('inspect', '--format', '{{json .Config.Labels}}', $containerName)
        $labels = @{}
        if ($inspect.Success) {
            try {
                $parsed = $inspect.StdOut.Trim() | ConvertFrom-Json
                foreach ($prop in $parsed.PSObject.Properties) { $labels[$prop.Name] = $prop.Value }
            }
            catch {
                Write-Verbose "Could not parse labels for '$containerName': $($_.Exception.Message)"
            }
        }

        $expiresAt = if ($labels.ContainsKey('albuild.expiresAt')) { $labels['albuild.expiresAt'] } else { '' }
        [PSCustomObject]@{
            Name      = $containerName
            Type      = if ($labels.ContainsKey('albuild.environmentType')) { $labels['albuild.environmentType'] } else { '' }
            Owner     = if ($labels.ContainsKey('albuild.owner')) { $labels['albuild.owner'] } else { '' }
            Url       = if ($labels.ContainsKey('albuild.publicUrl')) { $labels['albuild.publicUrl'] } else { '' }
            ExpiresAt = $expiresAt
            Expired   = Test-BcEnvironmentExpired -ExpiresAt $expiresAt
            Running   = ([string]$container.Status -like 'Up*')
        }
    }
}