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 and the organizational unit access token exists
    Assert-GSuiteConnection -Scope "OrganizationalUnit"

    # 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
}