Functions/Instances/Get-CdsInstances.ps1

<#
    .SYNOPSIS
    Retrieve instances collection.
#>

function Get-CdsInstances {
    [CmdletBinding()]
    param
    (        
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
        Assert-CdsIsAdmin;
    }    
    process {
        
        $cdsInstances = @();

        $environments = Get-PowerAppEnvironment;
        $environments | ForEach-Object {          
            $internalProperties = $_.Internal.properties;
            $metadata = $_.Internal.properties.linkedEnvironmentMetadata;

            $cdsInstance = New-CdsInstance; 
            $cdsInstance.Id = $_.EnvironmentName;
            $cdsInstance.Name = $metadata.domainName;
            $cdsInstance.UniqueName = $metadata.uniqueName;
            $cdsInstance.DisplayName = $_.DisplayName;       
            $cdsInstance.Url = $metadata.instanceUrl;
            $cdsInstance.ApiUrl = $metadata.instanceApiUrl;
            $cdsInstance.TenantId = [Guid]::Empty;
            $cdsInstance.Location = $_.Location;
            $cdsInstance.DataCenter = $internalProperties.azureRegionHint;
            $cdsInstance.Sku = $internalProperties.environmentSku;
            $cdsInstance.Type = $metadata.type;
            $cdsInstance.Version = $metadata.version;
            $cdsInstance.BaseLanguage = $metadata.baseLanguage;
            $cdsInstance.State = $metadata.instanceState;
            $cdsInstance.CreationTemplates = $metadata.creationTemplates;
            $cdsInstance.ConnectionString =  [String]::Empty;
            $cdsInstance.ParentConnection =  $null;

            $cdsInstance.ConnectionString = $cdsInstance | Out-CdsConnectionString; # TODO : Handle connectionstring according to auth context
            $cdsInstances += $cdsInstance;
        }            
        $cdsInstances;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Get-CdsInstances -Alias *;