Functions/Instances/Get-CdsInstances.ps1

<#
    .SYNOPSIS
    Retrieve instances collection.
#>

function Get-CdsInstances {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $false)]
        [ArgumentCompleter( { Get-CdsRegions })]
        [String]
        $Region = $Global:CdsContext.Region,

        [Parameter(Mandatory = $false)]
        [ArgumentCompleter( { Get-CdsAuthTypes })]
        [string]
        $AuthType = $Global:CdsContext.AuthType,
        
        [Parameter(Mandatory = $false)]
        [PsCredential]
        $Credentials = $Global:CdsContext.Credentials
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        
        $cdsInstances = @();

        if($AuthType -eq "Office365")
        {
            $instances = Get-CrmOrganizations -Credential $Credentials -DeploymentRegion $Region -OnLineType $AuthType; 
            
            $instances | ForEach-Object {
                $cdsInstance = New-CdsInstance;
                $cdsInstance.Id = $_.OrganizationId;
                $cdsInstance.Name = $_.UrlHostName;
                $cdsInstance.UniqueName = $_.UniqueName;
                $cdsInstance.DisplayName = $_.FriendlyName;
                $cdsInstance.Url = $_.WebApplicationUrl;
                $cdsInstance.ApiUrl = $_.OrganizationWebAPIUrl;
                $cdsInstance.TenantId = $_.TenantId;
                $cdsInstance.EnviromentId = $_.EnviromentId; 
                $cdsInstance.EnviromentName = $_.EnviromentId; # Align terminology with Microsoft.PowerApps.Administration.PowerShell module

                $cdsInstance.ConnectionString = $cdsInstance | Out-CdsConnectionString;
                $cdsInstances += $cdsInstance;
        }    
        }
        else {
            $instances = Get-PowerAppEnvironment;

            $instances | ForEach-Object {
                $cdsInstance = New-CdsInstance;
                $cdsInstance.Id = $_.resourceId;
                $cdsInstance.Name = $_.domainName;
                $cdsInstance.UniqueName = $_.uniqueName;
                $cdsInstance.DisplayName = $_.friendlyName;
                $cdsInstance.Url = $_.instanceUrl;
                $cdsInstance.ApiUrl = $_.instanceApiUrl;
                # $cdsInstance.TenantId = $_.TenantId; # TODO : Retrieve tenant from context
                $cdsInstance.EnviromentId = $_.EnvironmentName; 
                $cdsInstance.EnviromentName = $_.EnvironmentName; # Align terminology with Microsoft.PowerApps.Administration.PowerShell module

                $cdsInstance.ConnectionString = $cdsInstance | Out-CdsConnectionString;
                $cdsInstances += $cdsInstance;
        }

        
        $cdsInstances;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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