Skip to content

Commit 61f29e6

Browse files
authored
Update Get-EnvironmentsWithoutOrSingleAdmin.ps1
Add examples + formatting
1 parent 31f73b8 commit 61f29e6

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed
Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
11
<#
22
.SYNOPSIS
3-
Returns all hard-to-manage environments of an Azure DevOps organization
3+
Returns all hard-to-manage environments of an Azure DevOps organization
4+
45
.DESCRIPTION
5-
An Azure DevOps automation script that returns all environments of an Azure DevOps organization without an administrator or with only a single user in administrator role
6+
An Azure DevOps automation script that returns all environments of an Azure DevOps organization without an administrator or with only a single user in administrator role
7+
68
.PARAMETER PersonalAccessToken
7-
Azure DevOps personal access token (PAT) with the following scopes: User Profile (Read), Project and Team (Read), Build (Read), Environment (Read & manage)
9+
Azure DevOps personal access token (PAT) with the following scopes: User Profile (Read), Project and Team (Read), Build (Read), Environment (Read & manage)
10+
811
.PARAMETER OrganizationName
9-
Name of the Azure DevOps organization
12+
Name of the Azure DevOps organization
13+
1014
.PARAMETER PrintToConsole
11-
If set to true, the output will be printed to the console
15+
If set to true, the output will be printed to the console
16+
1217
.INPUTS
13-
None
18+
None
19+
1420
.OUTPUTS
15-
The environments of the given Azure DevOps organization without an administrator or with only a single user in administrator role
21+
The environments of the given Azure DevOps organization without an administrator or with only a single user in administrator role
22+
1623
.NOTES
17-
Version: 1.0
18-
Author: Marc Rufer
19-
Creation Date: 02.01.2024
20-
Purpose/Change: Initial script development
24+
Version: 1.0
25+
Author: Marc Rufer
26+
Creation Date: 02.01.2024
27+
Purpose/Change: Initial script development
28+
29+
.EXAMPLE
30+
PS> .\Get-EnvironmentsWithoutOrSingleAdmin.ps1 -PersonalAccessToken "PAT_HERE" -OrganizationName "ORGANIZATION_NAME_HERE" -PrintToConsole
31+
32+
.EXAMPLE
33+
PS> .\Get-EnvironmentsWithoutOrSingleAdmin.ps1 -PersonalAccessToken "PAT_HERE" -OrganizationName "ORGANIZATION_NAME_HERE"
2134
#>
2235
PARAM
2336
(
2437
[Parameter(Mandatory = $true, Position = 0, HelpMessage="Azure DevOps personal access token (PAT) with scopes: User Profile (Read), Project and Team (Read), Build (Read), Environment (Read & manage).")]
25-
[string] $PersonalAccessToken
38+
[string] $PersonalAccessToken
2639
,
2740
[Parameter(Mandatory = $true, Position = 1)]
2841
[string] $OrganizationName
29-
,
30-
[Parameter(Mandatory = $false, Position = 2)]
31-
[switch] $PrintToConsole = $false
42+
,
43+
[Parameter(Mandatory = $false, Position = 2)]
44+
[switch] $PrintToConsole = $false
3245
)
3346

3447
$base64encodedPAT = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("`:$PersonalAccessToken"))
@@ -45,30 +58,30 @@ foreach ($project in $projects) {
4558

4659
$envs = $response.value
4760
foreach ($env in $envs) {
48-
$uri = "https://dev.azure.com/{0}/_apis/securityroles/scopes/distributedtask.environmentreferencerole/roleassignments/resources/{1}_{2}?api-version=7.1-preview.1" -f $Organizationname, $project.id, $env.id
49-
$response = Invoke-RestMethod -Method Get -Uri $uri -Headers @{'Authorization' = "Basic $base64encodedPAT" }
50-
51-
$assignmentsToAdministratorRole = $response.value | ? { $_.role.identifier -eq "distributedtask.environmentreferencerole.Administrator" }
52-
53-
# add to result list, if no administrator role assignment exists
54-
if ($assignmentsToAdministratorRole.Count -eq 0) {
55-
$null = $environments.Add($env)
56-
# add to result list, if only a single user is assigned to the administrator role
57-
} elseif ($assignmentsToAdministratorRole -ne $null -and $assignmentsToAdministratorRole.Count -eq $null -and $assignmentsToAdministratorRole.identity.displayName -notcontains "\") {
58-
$null = $environments.Add($env)
59-
} else {
60-
Write-Host "Environment '$($env.name)' ($($env.id)) has more than one user assigned to the administrator role" -ForegroundColor Yellow
61-
}
61+
$uri = "https://dev.azure.com/{0}/_apis/securityroles/scopes/distributedtask.environmentreferencerole/roleassignments/resources/{1}_{2}?api-version=7.1-preview.1" -f $Organizationname, $project.id, $env.id
62+
$response = Invoke-RestMethod -Method Get -Uri $uri -Headers @{'Authorization' = "Basic $base64encodedPAT" }
63+
64+
$assignmentsToAdministratorRole = $response.value | ? { $_.role.identifier -eq "distributedtask.environmentreferencerole.Administrator" }
65+
66+
# add to result list, if no administrator role assignment exists
67+
if ($assignmentsToAdministratorRole.Count -eq 0) {
68+
$null = $environments.Add($env)
69+
# add to result list, if only a single user is assigned to the administrator role
70+
} elseif ($assignmentsToAdministratorRole -ne $null -and $assignmentsToAdministratorRole.Count -eq $null -and $assignmentsToAdministratorRole.identity.displayName -notcontains "\") {
71+
$null = $environments.Add($env)
72+
} else {
73+
Write-Host "Environment '$($env.name)' ($($env.id)) has more than one user assigned to the administrator role" -ForegroundColor Yellow
74+
}
6275
}
6376
}
6477

6578
if ($PrintToConsole) {
66-
Write-Host "Azure DevOps organization: $OrganizationName" -ForegroundColor Green
67-
Write-Host ("Projects count: {0}" -f $projects.Count) -ForegroundColor Green
68-
Write-Host ("Environments without or with single admin: {0}" -f $envs.Count) -ForegroundColor Green
69-
Write-Host ""
79+
Write-Host "Azure DevOps organization: $OrganizationName" -ForegroundColor Green
80+
Write-Host ("Projects count: {0}" -f $projects.Count) -ForegroundColor Green
81+
Write-Host ("Environments without or with single admin: {0}" -f $envs.Count) -ForegroundColor Green
82+
Write-Host ""
7083

71-
$environments | Format-Table -AutoSize -Wrap -Property id, name, @{Name="projectId"; Expression={$_.project.id}}, @{Name="projectName"; Expression={($projects |? id -eq $_.project.id).name}}
84+
$environments | Format-Table -AutoSize -Wrap -Property id, name, @{Name="projectId"; Expression={$_.project.id}}, @{Name="projectName"; Expression={($projects |? id -eq $_.project.id).name}}
7285
}
7386

7487
return $environments

0 commit comments

Comments
 (0)