Auth.ps1


#
# Authentication to Citrix Cloud
#

Function AuthToCitrixCloud([string]$customerId, [string]$secureClientId, [string]$secureSecret) {
    $parameters = @{
        CustomerId = $customerId
        Verbose = $Global:LogVerbose
    }
    if ([string]::IsNullOrWhiteSpace($secureClientId) -Or [string]::IsNullOrWhiteSpace($secureSecret)) {
        LogIt "Interactively authenticating for Citrix customer $customerId."
    }
    else {
        LogIt "Authenticating for Citrix customer $customerId using API key $secureClientId."
        Set-XDCredentials -CustomerId $customerId -APIKey $secureClientId -SecretKey $secureSecret -ProfileType CloudAPI
    }
    try {
        Get-XDAuthentication @parameters
        LogIt "Authentication successful."
        $credsList = Get-XDCredentials
        $creds = $credsList | Where-Object { $_.customerid -eq $customerId }
        if ($creds.Length -gt 0) {
            if ($creds[0].PSObject.Properties.Name -contains 'BearerToken' -and $creds[0].BearerToken) {
                $GLOBAL:XDAuthToken = $creds[0].BearerToken
                LogIt "Set BearerToken."
            }
        } else {
            LogIt "No credentials found for customerid: $customerId"
        }
    }
    catch {
        LogIt "Unable to authenticate to Citrix Cloud: $_"
        throw
    }
    LogIt "Authenticated for Citrix customer $customerId."
    return $parameters
}