Get a list of all users
The example below shows how to login to the API and retrieve a list of all users in the people directory.
Add-Type -AssemblyName System.Net.Http
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$username = "{username}"
$password = "{password}"
$tenantGuid = "{tenantguid}"
$apiuri = "{apiurl}"
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 -Body $log -ContentType $content -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
Updated almost 7 years ago