HomeREST APIGet a list of all users

Get a list of all users

Authenticate to the REST API and retrieve a paged list of all users in the people directory.

Before you start

  • You need the tenant GUID and API URL for your instance. See How to get API information.
  • You need a username and password to authenticate.

Overview

The example below logs in to the API and retrieves a list of all users in the people directory, paging through the results in batches of 50.

The script

Add-Type -AssemblyName System.Net.Http
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$username = "{{username}}"
$password = "{{password}}"
$tenantGuid = "{{tenant_guid}}"
$apiuri = "{{api_url}}"

function LogIn(){
    # Write a syncoption element using the passed values
    $log = @{
        "grant_type" = "password"
        "username" = $username
        "password" = $password}

    $header = @{
        "X-Tenant" = $tenantGuid}

    $api = Invoke-Restmethod -Uri ($apiuri + "/token") -Method post -Body $log -ContentType "application/x-www-form-urlencoded" -Headers $header

    return $api.access_token
}

function GetPeople([string] $token){
    $authenticateHeader = @{
        "X-Tenant" = $tenantGuid
        "Authorization" = "Bearer " + $token}

    $peopleUri = ($apiuri + "/api/people")

    # Initialise offset value at 0
    $offset = 0
    $limit = 50
    $continue = $true

    while($continue){
        $uriQ = $peopleUri + "?limit=" + $limit + "&offset=" + $offset

        # Launch + access API
        $people = Invoke-RestMethod -Uri $uriQ -Method GET -Headers $authenticateHeader

        foreach($person in $people.results){
            ProcessPerson $person
        }

        if($people.results.count -ne $limit){
          $continue = $false;
        }

        $offset += $limit
    }
}

function ProcessPerson($person){
    #Do something to the person
    Write-Host $person.FullName;
}

$token = LogIn
GetPeople $token

How it works

  • LogIn posts the username, password and grant_type=password to the /token endpoint with the X-Tenant header, and returns the access token.
  • GetPeople calls the /api/people endpoint with the token in the Authorization header, paging through results using the limit and offset query parameters until fewer than limit results are returned.
  • ProcessPerson is where you handle each returned person.
Section: REST API