|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Get a list of groups from Azure DevOps |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Get a list of groups from Azure DevOps for the specified organization and project using the Azure DevOps Rest API |
| 7 | +
|
| 8 | + .PARAMETER Project |
| 9 | + The name of the project to get groups for |
| 10 | +
|
| 11 | + .EXAMPLE |
| 12 | + Get-AzDevOpsGroups -Project 'MyProject' |
| 13 | +
|
| 14 | + .NOTES |
| 15 | + This function requires a connection to Azure DevOps. See Connect-AzDevOps for more information. |
| 16 | +#> |
| 17 | +Function Get-AzDevOpsGroups { |
| 18 | + [CmdletBinding()] |
| 19 | + param ( |
| 20 | + [Parameter(Mandatory=$true)] |
| 21 | + [string] |
| 22 | + $Project |
| 23 | + ) |
| 24 | + if($null -eq $script:connection) { |
| 25 | + throw 'Not connected to Azure DevOps. Run Connect-AzDevOps first.' |
| 26 | + } |
| 27 | + # Get project id |
| 28 | + try { |
| 29 | + $projectResult = Get-AzDevOpsProject -Project $Project |
| 30 | + } |
| 31 | + catch { |
| 32 | + throw "Failed to get project details from Azure DevOps" |
| 33 | + } |
| 34 | + $header = $script:connection.GetHeader() |
| 35 | + $Organization = $script:connection.Organization |
| 36 | + # Set the scope descriptor REST API endpoint |
| 37 | + $uri = "https://vssps.dev.azure.com/$($Organization)/_apis/graph/descriptors/$($projectResult.id)?api-version=7.2-preview.1" |
| 38 | + $scopeDescriptor = (Invoke-RestMethod -Uri $uri -Method Get -Headers $header).value |
| 39 | + $uri = "https://vssps.dev.azure.com/$($Organization)/_apis/graph/groups?scopeDescriptor=$scopeDescriptor&api-version=7.2-preview.1" |
| 40 | + $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $header |
| 41 | + return $response.value |
| 42 | +} |
| 43 | +Export-ModuleMember -Function Get-AzDevOpsGroups |
| 44 | +# End of Function Get-AzDevOpsGroups |
| 45 | + |
| 46 | +<# |
| 47 | + .SYNOPSIS |
| 48 | + Get details for a group from Azure DevOps |
| 49 | +
|
| 50 | + .DESCRIPTION |
| 51 | + Get details for a group from Azure DevOps for the specified group object using the Azure DevOps Rest API |
| 52 | +
|
| 53 | + .PARAMETER Group |
| 54 | + The group object to get details for |
| 55 | +
|
| 56 | + .EXAMPLE |
| 57 | + $group = (Get-AzDevOpsGroups -Project 'MyProject')[0] |
| 58 | + Get-AzDevOpsGroupDetails -Group $group |
| 59 | +
|
| 60 | + .NOTES |
| 61 | + This function requires a connection to Azure DevOps. See Connect-AzDevOps for more information. |
| 62 | +#> |
| 63 | +Function Get-AzDevOpsGroupDetails { |
| 64 | + [CmdletBinding()] |
| 65 | + param ( |
| 66 | + [Parameter(Mandatory=$true)] |
| 67 | + [object] |
| 68 | + $Group |
| 69 | + ) |
| 70 | + if($null -eq $script:connection) { |
| 71 | + throw 'Not connected to Azure DevOps. Run Connect-AzDevOps first.' |
| 72 | + } |
| 73 | + $header = $script:connection.GetHeader() |
| 74 | + $result = $Group |
| 75 | + |
| 76 | + # Get detail for which group this group is a member of |
| 77 | + $memberShipUri = $Group._links.memberships.href |
| 78 | + try { |
| 79 | + $membershipResult = (Invoke-RestMethod -Uri $memberShipUri -Method Get -Headers $header).value |
| 80 | + if($membershipResult -is [string] -or $null -eq $membershipResult) { |
| 81 | + throw "Authentication failed or organization not found" |
| 82 | + } |
| 83 | + } |
| 84 | + catch { |
| 85 | + throw "Failed to get group memberOf details from Azure DevOps" |
| 86 | + } |
| 87 | + # Get the self information for each membership |
| 88 | + $memberships = @() |
| 89 | + foreach($item in $membershipResult) { |
| 90 | + $itemUri = $item._links.container.href |
| 91 | + $itemResult = (Invoke-RestMethod -Uri $itemUri -Method Get -Headers $header) |
| 92 | + $memberships += $itemResult |
| 93 | + } |
| 94 | + $result | Add-Member -MemberType NoteProperty -Name MemberOf -Value $memberships -Force |
| 95 | + |
| 96 | + # Get all members of this group |
| 97 | + $memberUri = "$($Group._links.memberships.href)?direction=down&api-version=7.2-preview.1" |
| 98 | + $memberResult = (Invoke-RestMethod -Uri $memberUri -Method Get -Headers $header).value |
| 99 | + |
| 100 | + # Get the self information for each member |
| 101 | + $members = @() |
| 102 | + foreach($item in $memberResult) { |
| 103 | + $itemUri = $item._links.member.href |
| 104 | + $itemResult = (Invoke-RestMethod -Uri $itemUri -Method Get -Headers $header) |
| 105 | + $members += $itemResult |
| 106 | + } |
| 107 | + $result | Add-Member -MemberType NoteProperty -Name Members -Value $members -Force |
| 108 | + return $result |
| 109 | +} |
| 110 | +Export-ModuleMember -Function Get-AzDevOpsGroupDetails |
| 111 | + |
| 112 | +<# |
| 113 | + .SYNOPSIS |
| 114 | + Export all groups from Azure DevOps for a project to a JSON file |
| 115 | +
|
| 116 | + .DESCRIPTION |
| 117 | + Export all groups from Azure DevOps for a project to a JSON file |
| 118 | +
|
| 119 | + .PARAMETER Project |
| 120 | + The name of the project to get groups for |
| 121 | +
|
| 122 | + .PARAMETER OutputPath |
| 123 | + The folder path to the JSON file to export to |
| 124 | +
|
| 125 | + .EXAMPLE |
| 126 | + Export-AzDevOpsGroups -Project 'MyProject' -OutputPath 'C:\Temp\' |
| 127 | +
|
| 128 | + .NOTES |
| 129 | + This function requires a connection to Azure DevOps. See Connect-AzDevOps for more information. |
| 130 | +#> |
| 131 | +Function Export-AzDevOpsGroups { |
| 132 | + [CmdletBinding()] |
| 133 | + param ( |
| 134 | + [Parameter(Mandatory=$true)] |
| 135 | + [string] |
| 136 | + $Project, |
| 137 | + [Parameter(Mandatory=$true)] |
| 138 | + [string] |
| 139 | + $OutputPath |
| 140 | + ) |
| 141 | + if($null -eq $script:connection) { |
| 142 | + throw 'Not connected to Azure DevOps. Run Connect-AzDevOps first.' |
| 143 | + } |
| 144 | + try { |
| 145 | + $groups = Get-AzDevOpsGroups -Project $Project |
| 146 | + } |
| 147 | + catch { |
| 148 | + throw "Failed to get groups from Azure DevOps" |
| 149 | + } |
| 150 | + $groupDetails = @() |
| 151 | + foreach($group in $groups) { |
| 152 | + $thisGroup = Get-AzDevOpsGroupDetails -Group $group |
| 153 | + # Add an ObjectType property to the group object |
| 154 | + $thisGroup | Add-Member -MemberType NoteProperty -Name ObjectType -Value 'Azure.DevOps.Group' -Force |
| 155 | + # Add the group name to the group object as an ObjectName property with a convention of Organization.Project.GroupName |
| 156 | + $thisGroup | Add-Member -MemberType NoteProperty -Name ObjectName -Value "$($script:connection.Organization).$($Project).$($group.displayName)" -Force |
| 157 | + |
| 158 | + $groupDetails += $thisGroup |
| 159 | + } |
| 160 | + $groupDetails | ConvertTo-Json -Depth 100 | Out-File -FilePath "$OutputPath\groups.ado.json" |
| 161 | +} |
| 162 | +Export-ModuleMember -Function Export-AzDevOpsGroups |
| 163 | +# End of Function Export-AzDevOpsGroups |
0 commit comments