Created
September 30, 2022 15:39
-
-
Save plmcgrn/c0535af65da6e58d60ceb03dfb646954 to your computer and use it in GitHub Desktop.
Script to get all users in a Genesys Cloud organization
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
#set to the appropriate org URL. Defaults to US East/mypurecloud.com | |
$cloudenv = 'mypurecloud.com' | |
#Set credentials | |
$clientid = 'OAuth Client ID' | |
$clientsecret = 'OAuth Client Secret' | |
#set extra properties to pull from API | |
$extendedproperties = "skills,groups,authorization,dateLastLogin,lasttokenissued,manager" | |
$pair = "$($clientid):$($clientsecret)" | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) | |
$base64 = [System.Convert]::ToBase64String($bytes) | |
#Get OAuth token | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("Authorization", "Basic $base64") | |
$headers.Add("Content-Type", "application/x-www-form-urlencoded") | |
$body = "grant_type=client_credentials" | |
Write-Output "Logging in to Genesys Cloud API" | |
try { | |
$response = Invoke-RestMethod "https://login.$cloudenv/oauth/token" -Method 'POST' -Headers $headers -Body $body | |
} | |
catch { | |
Write-Output "Failed to log in to Genesys Cloud API. Aborting." | |
Write-Output $_ | |
Write-Error "Failed to log in to Genesys Cloud API. Aborting." | |
Write-Error $_ | |
Exit 1 | |
} | |
#set bearer based on returned access token | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("Authorization", "Bearer $($response.access_token)") | |
$users = Invoke-RestMethod "https://api.$cloudenv/api/v2/users?expand=$($extendedproperties)&pageSize=200" -Method 'GET' -Headers $headers -Verbose | |
#add users to separate collection, in case we have more pages to fetch | |
$userlist = [System.Collections.ArrayList]::new() | |
foreach ($u in $users.entities) { | |
$userlist.add($u) | Out-Null | |
} | |
#see if we have additional pages of users, if so, get them all | |
if ($false -eq [string]::IsNullOrEmpty($users.nextUri)) { | |
Write-Output "User list has multiple pages ($($users.pageCount))" | |
$selfUri = $users.selfUri | |
$nextUri = $users.nextUri | |
$lastUri = $users.lastUri | |
while ($selfUri -ne $lastUri) { | |
Write-Output "Getting next page of users ($($users.pageNumber + 1))" | |
$users = Invoke-RestMethod "https://api.$($cloudenv)$($nextUri)" -Method 'GET' -Headers $headers | |
#add additional users to collection | |
foreach ($u in $users.entities) { | |
$userlist.add($u) | Out-Null | |
} | |
$selfUri = $users.selfUri | |
$nextUri = $users.nextUri | |
} | |
} | |
$users = $userlist | Sort-Object -Property Name | |
$usersexport = [System.Collections.ArrayList]::new() | |
$ofs = ',' #change output field separator to comma, to make casts easier | |
foreach ($u in $userlist) { | |
Write-Output "Getting queues for $($u.email)" | |
$queues = Invoke-RestMethod "https://api.$($cloudenv)/api/v2/users/$($u.id)/queues" -Method 'GET' -Headers $headers | |
Write-Output "$($u.email): Queues - $($queues.entities.name)" | |
foreach ($q in $queues.entities) { | |
$queuelookup = $q | Where-Object -Property id -eq $q.id | |
$q.Name = $queuelookup.name | |
} | |
$u | Add-Member -NotePropertyName queues -NotePropertyValue $queues | |
$user = [PSCustomObject]@{ | |
name = $u.name; | |
email = $u.email | |
#TODO: Group memberships will require some re-work, the /api/v2/users doesn't return the group names | |
groupsCount = $u.groups.count | |
department = $u.department | |
title = $u.title | |
#TODO: match manager from initial user list so we don't make another API call | |
#manager = $u.manager.name | |
skills = [String]($u.skills.name | Sort-Object) | |
skillsCount = $u.skills.count | |
queues = [String]($queues.entities.name | Sort-Object) | |
roles = [String]($u.authorization.roles.name | Sort-Object) | |
rolesCount = $u.authorization.roles.count | |
dateLastLogin = $u.dateLastLogin | |
lastTokenIssued = $u.lastTokenIssued.dateIssued | |
} | |
$usersexport.add($user) | Out-Null | |
Start-Sleep -Milliseconds 600 | |
} | |
$ofs = ' ' #reset output field separator back to space/default | |
$usersexport | Format-Table | |
$usersexport | Export-Csv -Path "~/Desktop/$($cloudenv)-cloudusers.csv" -NoTypeInformation |
$u.division.name should do it
$u.division.name should do it
That's worked, thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you so much for this very useful tool! I have tried adding the Division as an additional column to be exported by adding "division" to the list of extended properties and also adding "division = $u.division" to the PSCustomObject. It almost works perfectly but instead of just outputting the division name, I am getting in this format "@{id=DivisionID; name=DivisionName; selfUri=/api/v2/authorization/divisions/DivisionID}".
Is there a way I can get it so it only outputs the Division name into the .csv file?