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
        
        $success = $false;
        if ($PSBoundParameters.ContainsKey('UserName')) {
            $securePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force;
            Add-PowerAppsAccount -Username $UserName -Password  $securePassword;
            $success = $true;
        }
        elseif ($PSBoundParameters.ContainsKey('ClientSecret')) {
            Add-PowerAppsAccount -TenantID $TenantId -ApplicationId $ApplicationId -ClientSecret $ClientSecret;
            $success = $true;
        }
        elseif ($PSBoundParameters.ContainsKey('CertificateThumbprint')) {
            Add-PowerAppsAccount -TenantID $TenantId -ApplicationId $ApplicationId -CertificateThumbprint  $CertificateThumbprint;
            $success = $true;
        }
        else {
            Add-PowerAppsAccount;
            $success = $true;
        }

        if(-not $Global:CdsContext)
        {            
            $Global:CdsContext = New-CdsContext; 
        }
        $Global:CdsContext.IsAdminConnected = $success;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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