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
LogInposts the username, password andgrant_type=passwordto the/tokenendpoint with theX-Tenantheader, and returns the access token.GetPeoplecalls the/api/peopleendpoint with the token in theAuthorizationheader, paging through results using thelimitandoffsetquery parameters until fewer thanlimitresults are returned.ProcessPersonis where you handle each returned person.
Related
Section: REST API