Functions/Administration/Connect-CdsAdmin.ps1

<#
    .SYNOPSIS
    Use Add-PowerAppsAccount cmdlet signs in the user or application account and saves the sign in information to cache.
#>

function Connect-CdsAdmin {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false)]
        [String]
        $UserName,

        [Parameter(Mandatory=$false)]
        [String]
        $Password,

        [Parameter(Mandatory=$false)]
        [String]
        $TenantId,

        [Parameter(Mandatory=$false)]
        [String]
        $ApplicationId,

        [Parameter(Mandatory=$false)]
        [String]
        $ClientSecret,

        [Parameter(Mandatory=$false)]
        [String]
        $CertificateThumbprint  
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);        
    }    
    process {    
        # This is just a wrapper for Power Apps admin connection
        # It could be done differently
        # I don't know if endpoint or audience are usefull here
        # https://docs.microsoft.com/en-us/powershell/module/microsoft.powerapps.administration.powershell/add-powerappsaccount?view=pa-ps-latest
        
        # Force disconnect to refresh tocken
        Remove-PowerAppsAccount;
          
        $Global:CdsContext = New-CdsContext; 
        $Global:CdsContext.IsOnline = $true;
        $Global:CdsContext.IsOnPremise = $false;

        $success = $false;
        if ($PSBoundParameters.ContainsKey('UserName')) {
            
            # Set Credential object required authentications
            $credentials = Set-CdsCredentials -Login $UserName -Password $Password;        
            $securePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force;
            Add-PowerAppsAccount -Username $UserName -Password $securePassword -Endpoint prod;
            
            $cdsConnection = New-CdsConnection;   
            $cdsConnection.AuthType = "Office365"; # TODO : Ifd ?
            $cdsConnection.UserName = $UserName;
            $cdsConnection.Password = $Password;
            $cdsConnection.Credentials = $credentials;
            $Global:CdsContext.CurrentConnection =  $cdsConnection;

            $success = $true;
        }
        elseif ($PSBoundParameters.ContainsKey('ClientSecret')) {
            Add-PowerAppsAccount -TenantID $TenantId -ApplicationId $ApplicationId -ClientSecret $ClientSecret -Endpoint prod;

            $cdsConnection = New-CdsConnection;              
            $cdsConnection.AuthType = "ClientSecret"; 
            $cdsConnection.TenantId = $TenantId;
            $cdsConnection.ApplicationId = $ApplicationId;
            $cdsConnection.ClientSecret = $ClientSecret;
            $Global:CdsContext.CurrentConnection =  $cdsConnection;

            $success = $true;
        }
        elseif ($PSBoundParameters.ContainsKey('CertificateThumbprint')) {
            Add-PowerAppsAccount -TenantID $TenantId -ApplicationId $ApplicationId -CertificateThumbprint $CertificateThumbprint -Endpoint prod;

            $cdsConnection = New-CdsConnection;              
            $cdsConnection.AuthType = "Certificate"; 
            $cdsConnection.TenantId = $TenantId;
            $cdsConnection.ApplicationId = $ApplicationId;
            $cdsConnection.CertificateThumbprint = $CertificateThumbprint;
            $Global:CdsContext.CurrentConnection =  $cdsConnection;

            $success = $true;
        }
        else {
            # Add-PowerAppsAccount;
            # TODO : Handle manual auth scenario
            $success = $false;
        }
        $Global:CdsContext.IsAdminConnected = $success;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Connect-CdsAdmin -Alias *;