Private/PartnerActions/Get-SAMTokens.ps1

function Get-SAMTokens() {
    # Start by connecting to our Azure Key Vault.
    $AzContext = Get-AzContext
    if (!$AzContext -or $AzContext.Tenant.Id -ne $PartnerTenantId) {
        try {
            Connect-AzAccount -Tenant $PartnerTenantId -SubscriptionName $SubscriptionName | Out-Null
        }
        catch {
            Write-Error "Failed to connect to Azure. Please make sure you have the Az module installed."
        }
    }

    try {
        # Retreive all required values from Azure Key Vault
        $ApplicationId = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name "ApplicationId" -AsPlainText
        $ApplicationSecret = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name "ApplicationSecret" -AsPlainText
        $RefreshToken = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name "RefreshToken" -AsPlainText
        $ExchangeRefreshToken = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name "ExchangeRefreshToken" -AsPlainText

        $ApplicationCredential = (New-Object System.Management.Automation.PSCredential ($ApplicationId, (ConvertTo-SecureString $ApplicationSecret -AsPlainText -Force)))
    }
    catch {
        Write-Error "Failed to connect to Azure Key Vault and retreive secrets."
    }

    return [PSCustomObject]@{
        ApplicationId         = $ApplicationId
        ApplicationSecret     = $ApplicationSecret
        RefreshToken          = $RefreshToken
        ExchangeRefreshToken  = $ExchangeRefreshToken
        ApplicationCredential = $ApplicationCredential
    }
}