PrivateFunctions/Assert-GSuiteConnection.ps1

<#
.SYNOPSIS
    This function checks if the access token for a certain scope is present.
    This function should be called before calling other GSuite functions.
#>

function Assert-GSuiteConnection {
    [CmdletBinding(PositionalBinding=$false)]
    param (
        # The scope to check.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$scope
    )

    # Validate that the 'connection' has been established
    if (!$Global:GSuiteAccessTokensHashTable) {
        throw "You must call the Connect-GSuiteAdminAccount cmdlet before calling any other GSuite cmdlets."
    }

    # Validate that the required access token exists in the hash table
    if ([String]::IsNullOrWhiteSpace($Global:GSuiteAccessTokensHashTable.$($scope))) {
        throw "$($scope) access token is required."
    }
}