Skip to content

Instantly share code, notes, and snippets.

@LockTar
Created July 14, 2021 11:30
Show Gist options
  • Save LockTar/9603e02cfbc3278c9d8b46bd0801c1e6 to your computer and use it in GitHub Desktop.
Save LockTar/9603e02cfbc3278c9d8b46bd0801c1e6 to your computer and use it in GitHub Desktop.
Azure DevOps UserGroups per user
Param
(
[PSCustomObject]$User,
[string]$Organization # https://dev.azure.com/foo
)
Write-Host "Get user groups for user: $($User.principalName) in organisation $Organization"
$UserGroups = @()
$activeUserGroups = az devops security group membership list --id $User.principalName --org $Organization --relationship memberof | ConvertFrom-Json
[array]$groups = ($activeUserGroups | Get-Member -MemberType NoteProperty).Name
foreach ($aug in $groups) {
$UserGroups += New-Object -TypeName PSObject -Property @{
principalName = $User.principalName
displayName = $User.displayName
GroupName = $activeUserGroups.$aug.principalName
}
}
$UserGroups
Param
(
[string]$PAT,
[string]$Organization # https://dev.azure.com/foo
)
$exportUsersJsonFilePath = "./ado-all-users.json"
$exportUserGroupsJsonFilePath = "./ado-user-groups-per-user.json"
# ========================================== Synchroon ====================================================
# $UserGroups = @()
# Write-Output $PAT | az devops login --org $Organization
# az devops configure --defaults organization=$Organization
# $allUsers = az devops user list --org $Organization | ConvertFrom-Json
# $allUsers.members | ConvertTo-Json -Depth 100 | Out-File -FilePath $exportUsersJsonFilePath
# foreach ($au in $allUsers.members) {
# $activeUserGroups = az devops security group membership list --id $au.user.principalName --org $Organization --relationship memberof | ConvertFrom-Json
# [array]$groups = ($activeUserGroups | Get-Member -MemberType NoteProperty).Name
# foreach ($aug in $groups) {
# $UserGroups += New-Object -TypeName PSObject -Property @{
# principalName = $au.user.principalName
# displayName = $au.user.displayName
# GroupName = $activeUserGroups.$aug.principalName
# }
# }
# }
# $UserGroups | ConvertTo-Json | Out-File -FilePath $exportUserGroupsJsonFilePath
# Write-Host "Exported user groups for all users in organisation $Organization to $exportUserGroupsJsonFilePath"
# ========================================== Asynchroon ====================================================
$ArtifactPath = "."
Write-Output $PAT | az devops login --org $Organization
az devops configure --defaults organization=$Organization
Write-Host "Get all users in organisation: $Organization"
$allUsers = az devops user list --org $Organization --top 10000 | ConvertFrom-Json
$allUsers.members | ConvertTo-Json -Depth 100 | Out-File -FilePath $exportUsersJsonFilePath
$users = Get-Content $exportUsersJsonFilePath | ConvertFrom-Json
Write-Host "Number of users in organisation are $($users.Count)"
$AllUserGroups = $users | Select-Object -First 1500 | ForEach-Object -Parallel {
$user = $_.user
& $using:ArtifactPath\Get-UserGroups.ps1 -User $user -Organization $using:Organization
} -ThrottleLimit 10
$AllUserGroups | ConvertTo-Json | Out-File -FilePath $exportUserGroupsJsonFilePath
Write-Host "Exported user groups for all users in organisation $Organization to $exportUserGroupsJsonFilePath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment