Functions/Get-GSuiteOrganizationalUnit.ps1

<#
.SYNOPSIS
    This function retrieves all organizational units for an account.
    This function returns either PSCustomObject when there is one organizational unit, or Object[] when there is more than one organizational unit.
#>

function Get-GSuiteOrganizationalUnit {
    [OutputType([Object[]], [PSCustomObject])]
    param()

    # 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 organizational unit access token exists in the hash table
    if ([String]::IsNullOrWhiteSpace($Global:GSuiteAccessTokensHashTable.OrganizationalUnit)) {
        throw "Organizational unit access token is required to call Get-GSuiteOrganizationalUnit."
    }

    # Prepare REST call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits"
        Method  = "GET"
        Headers = @{
            Accept        = "application/json"
            Authorization = "Bearer $($Global:GSuiteAccessTokensHashTable.OrganizationalUnit)"

        }
        Body    = @{
            type = "all"
        }
    }

    # Invoke the REST call
    $response = Invoke-RestMethod @invokeRestMethodParams

    # return all organizational units
    return $response.organizationUnits
}