Created
September 20, 2017 20:14
-
-
Save jdmills-edu/76810889d8812e347c8893304107a027 to your computer and use it in GitHub Desktop.
A PowerShell script that lists all Zendesk users in your tenant. You can use the role parameter to have the script return admins, agents, agents+admins, end-users, or all users. Supports pagination and deduplication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[ValidateSet('admin', 'agents', 'allagents', 'endusers', 'allusers')][String]$role | |
) | |
#Zendesk API Connection Headers Referencing System Environmental Variables for username and API token. | |
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($env:ZendeskAPI_Username):$($env:ZendeskAPI_Token)"));} | |
#GET ALL ZENDESK USERS | |
$allUsers = @() | |
$geturi = "https://yourdomain.zendesk.com/api/v2/users.json" | |
$users = Invoke-RestMethod -Uri $geturi -Method Get -Headers $headers -ContentType "application/json" | |
$pages = [math]::Ceiling($users.count/100) | |
for($i=0; $i -lt $pages; $i++){ | |
Write-Progress -Activity "Loading Zendesk users..." -PercentComplete (($i/$pages)*100) | |
$geturi = "https://yourdomain.zendesk.com/api/v2/users.json?page=$i" | |
$users = Invoke-RestMethod -Uri $geturi -Method Get -Headers $headers -ContentType "application/json" | |
$allUsers += $users.users | |
} | |
Write-Progress -Activity "Loading Zendesk users..." -Completed | |
#DEDUPE | |
$allUsers = $allUsers | select * -Unique | |
#CATEGORIZE INTO ADMINS, AGENTS, END USERS | |
$admins = $allUsers | where role -eq "admin" | |
$agents = $allUsers | where role -eq "agent" | |
$endusers = $allUsers | where role -eq "end-user" | |
switch($role){ | |
"admins"{ | |
return $admins | |
} | |
"agents"{ | |
return $agents | |
} | |
"allagents"{ | |
return $agents+$admins | |
} | |
"endusers"{ | |
return $endusers | |
} | |
default{ | |
return $allUsers | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment