-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_users_ManagementGroups.ps1
More file actions
93 lines (82 loc) · 3.94 KB
/
Get_users_ManagementGroups.ps1
File metadata and controls
93 lines (82 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Connect-AzAccount
# Get all subscriptions
$subscriptions = Get-AzSubscription
# Initialize an array to hold all role assignments
$allRoleAssignments = @()
# Function to get all users in a group, including nested groups
function Get-GroupMembers($groupId, $groupName) {
$members = Get-AzADGroupMember -GroupObjectId $groupId
$allMembers = @()
foreach ($member in $members) {
if ($member.ObjectType -eq "Group" -or $member.UserPrincipalName -like "*GRP*" -or $member.DisplayName -like "*GRP*") {
$nestedMembers = Get-GroupMembers -groupId $member.Id -groupName $groupName
$allMembers += $nestedMembers
} else {
$allMembers += [PSCustomObject]@{
UserPrincipalName = $member.UserPrincipalName
DisplayName = $member.DisplayName
GroupName = $groupName
}
}
}
return $allMembers
}
# Loop through each subscription
foreach ($subscription in $subscriptions) {
$subscriptionId = $subscription.SubscriptionId
$subscriptionName = $subscription.Name
# Set the context to the specified subscription
Set-AzContext -SubscriptionId $subscriptionId
# Define the scope at the subscription level
$scope = "/subscriptions/$subscriptionId"
# Get Azure RBAC role assignments at the subscription level
$roleAssignments = Get-AzRoleAssignment -Scope $scope
# Filter role assignments to exclude resource groups
$filteredRoleAssignments = $roleAssignments | Where-Object { $_.Scope -notlike "/subscriptions/*/resourceGroups/*" }
# Expand groups to list all users
foreach ($assignment in $filteredRoleAssignments) {
if ($assignment.ObjectType -eq "Group" -or $assignment.SignInName -like "*GRP*" -or $assignment.DisplayName -like "*GRP*") {
$groupMembers = Get-GroupMembers -groupId $assignment.ObjectId -groupName $assignment.DisplayName
if ($groupMembers.Count -eq 0) {
$allRoleAssignments += [PSCustomObject]@{
SubscriptionName = $subscriptionName
UPN = $assignment.SignInName
ObjectType = "Group"
RoleDefinitionName = $assignment.RoleDefinitionName
DisplayName = $assignment.DisplayName
GroupName = ""
Scope = "/subscriptions/$subscriptionId$($assignment.Scope)"
Commentary = "0 users or groups"
}
} else {
foreach ($member in $groupMembers) {
$allRoleAssignments += [PSCustomObject]@{
SubscriptionName = $subscriptionName
UPN = $member.UserPrincipalName
ObjectType = "User"
RoleDefinitionName = $assignment.RoleDefinitionName
DisplayName = $member.DisplayName
GroupName = $member.GroupName
Scope = "/subscriptions/$subscriptionId$($assignment.Scope)"
Commentary = ""
}
}
}
} else {
$allRoleAssignments += [PSCustomObject]@{
SubscriptionName = $subscriptionName
UPN = $assignment.SignInName
ObjectType = $assignment.ObjectType
RoleDefinitionName = $assignment.RoleDefinitionName
DisplayName = $assignment.DisplayName
GroupName = ""
Scope = "/subscriptions/$subscriptionId$($assignment.Scope)"
Commentary = ""
}
}
}
}
# Export the expanded role assignments to a CSV file
$csvFile = "expanded_role_assignments.csv"
$allRoleAssignments | Select-Object -Property SubscriptionName, UPN, ObjectType, RoleDefinitionName, DisplayName, GroupName, Scope, Commentary | Export-Csv -Path $csvFile -NoTypeInformation
Write-Output ("Expanded role assignments have been exported to " + $csvFile)