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);        
    }    
    process {
        
        $cdsInstances = @();
        
        if($Global:CdsContext.IsAdminConnected)
        {
            $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;
            }            
        }
        elseif($Global:CdsContext.IsUserConnected) {
            $instances = Get-CrmOrganizations -Credential $Global:CdsContext.CurrentConnection.Credentials -DeploymentRegion $Global:CdsContext.CurrentConnection.Region -OnLineType $Global:CdsContext.CurrentConnection.AuthType;
            
            $instances | ForEach-Object {
                $cdsInstance = New-CdsInstance;
                $cdsInstance.Id = $_.EnviromentId;
                $cdsInstance.Name = $_.UrlHostName;
                $cdsInstance.UniqueName = $_.UniqueName;
                $cdsInstance.DisplayName = $_.FriendlyName;
                $cdsInstance.Url = $_.WebApplicationUrl;
                $cdsInstance.ApiUrl = $_.OrganizationWebAPIUrl;
                $cdsInstance.TenantId = $_.TenantId;

                $cdsInstance.ConnectionString = $cdsInstance | Out-CdsConnectionString;  # TODO : Handle connectionstring according to auth context
                $cdsInstances += $cdsInstance;
            }
        }
        else {
            throw "You are not connected! Please use Connect-CdsUser or Connect-CdsAdmin command before."
        }
        $cdsInstances;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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