Public/FlyConnectApi.ps1

<#
.SYNOPSIS
 
Connect to Fly Public API
 
.DESCRIPTION
 
Connect to Fly Public API
 
.PARAMETER Url
The public API URL varies with your data center, you can find it in Fly user guide.
 
.PARAMETER ClientId
The application (client) ID you have retrieved from AvePoint Online Service App registrations.
 
.PARAMETER ClientSecret
The corresponding client secret you used while registering the AvePoint app.
 
.PARAMETER Certificate
The corresponding .pfx certificate file of the .cer certificate you used while registering the AvePoint app.
 
.OUTPUTS
 
None
#>

function Connect-Fly {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory = $true)]
        [String]
        ${Url},
        [Parameter(Position = 1, Mandatory = $true)]
        [String]
        ${ClientId},
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "ClientCertificate")]
        [Alias("Cert")]
        [System.Security.Cryptography.X509Certificates.X509Certificate2]
        ${Certificate},
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "ClientSecret")]
        [String]
        $ClientSecret,
        [Parameter(Mandatory = $false)]
        [String]
        ${IdentityServiceUri}
    )
    PROCESS {
        if (!$IdentityServiceUri) {
            $IdentityServiceUri = 'https://identity.avepointonlineservices.com'
            if ($Url -eq 'https://graph-gov.avepointonlineservices.com/fly') {
                $IdentityServiceUri = 'https://identity-gov.avepointonlineservices.com'
            }
            elseif ($Url.StartsWith('https://graph-public.sharepointguild.com/')) {
                $IdentityServiceUri = 'https://identity-public.sharepointguild.com'
            }
            elseif ($Url -eq 'https://flyusgovapi.online15.net') {
                $IdentityServiceUri = 'https://identity.online15.net'
            }
            elseif ($Url -eq 'https://flyapi.online15.net') {
                $IdentityServiceUri = 'https://identity-stage.online15.net'
            }
        }
        $Configuration = Get-FlyConfiguration
        $Token = ''
        switch ($PsCmdlet.ParameterSetName) {
            "ClientCertificate" {
                $Token = Get-IdentityServiceToken -IdentityServiceUri $IdentityServiceUri -Scope fly.graph.readwrite.all -ClientId $ClientId -Cert $Certificate
            }
            "ClientSecret" {
                $Token = Get-IdentityServiceTokenByClientSecret -IdentityServiceUri $IdentityServiceUri -Scope fly.graph.readwrite.all -ClientId $ClientId -ClientSecret $ClientSecret
            }
        }
        $CustomerId = (Convert-JWTtoken $Token).realm
        if ($Configuration.containsKey("CustomerId") -and $CustomerId -ne $Configuration["CustomerId"]) {
            $continue = Read-Host "Are you sure you want to connect to another tenant? (Y/N)"
            if ($continue -eq "N") {
                return;
            }
        }
        $Configuration["BaseUrl"] = $Url
        $Configuration["AccessToken"] = $Token
        $Configuration["IdentityServiceUri"] = $IdentityServiceUri
        $Configuration["ClientId"] = $ClientId
        $Configuration["CustomerId"] = $CustomerId
        $Configuration["Certificate"] = $Certificate
    }
}

<#
.SYNOPSIS
 
Disconnect from Fly Public API
 
.DESCRIPTION
 
Disconnect from Fly Public API
 
.OUTPUTS
 
None
#>

function Disconnect-Fly {
    [CmdletBinding()]
    Param()
    PROCESS {
        $Configuration = Get-FlyConfiguration
        $Configuration["BaseUrl"] = $null
        $Configuration["AccessToken"] = $null
        $Configuration["IdentityServiceUri"] = $null
        $Configuration["ClientId"] = $null
        $Configuration["CustomerId"] = $null
        $Configuration["Certificate"] = $null
    }
}