-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAD-Groups.ps1
More file actions
177 lines (168 loc) · 5.7 KB
/
AD-Groups.ps1
File metadata and controls
177 lines (168 loc) · 5.7 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Author - Scott P. Morton Ph.D.
# Date - on or about 4/20/2023
# Searches for circular memberships of groups
# See methods below, adjust the following parameters to suit
# your needs
#
# Copyright (C) 2023 Scott P. Morton PhD (spm3c at mtmail.mtsu.edu)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The output file 'cyclical-groups.csv' has 'DistinguishedName' (DN)
# and 'Zgroupo x' The DN is the searched group, each 'Zgroup x' is
# an endpoint group that creates a loop back to the DN,
# where x is 0 through maxZgroups parameter below.
#
# The output file 'max-depth-groups.txt' can be used to narrow the
# succesive searches to a limited number of groups that exceed
# maxDepth through method 3 to search for the 'bottom'
#
# The output file 'error-groups.txt' needs no explanation
# Specific details of any DistinguishedName you want
# to exclude from the search, like a foreign domain
$FD = "DC=adomain,DC=com"
# Maximum recursive depth, start shallow and work your way up
# or you'll be sorry
$script:depthLimit = 1
# Max number of endpoints to add to any group entry (DN) that has loops
$maxZgroups = 10
# dupGroups contains all cyclical groups
$dupGroups = @()
# errorGroups... duh
$script:errorGrpsp = @{}
# Any group that reaches max recursive depth
$script:depthGroupsm = @{}
# !!!! METHODS !!!!
# Identifies method to run:
# Full domain (1)
# Subset of domain (2)
# Single group (3)
# Single user (4)
$method = 3
if ($method -eq 1) {
$groups = Get-ADGroup -filter *
}
elseif ($method -eq 2) {
$groups = Get-ADGroup -filter "Name -like '*Citrix*'"
}
elseif ($method -eq 3) {
$id = "CN=anobject,OU=org1,OU=org2,DC=subdom,DC=dom,DC=net"
$groups = Get-ADGroup $id
}
elseif ($method -eq 4) {
$groups = @()
$x = Get-ADUser auser -Properties "MemberOf"
foreach ($id in $x["MemberOf"]) {
$groups += Get-ADGroup $id
}
}
else {
Write-Host "Invalid Method chosen, check 'method' value"
$groups = @()
}
function getGroups {
param (
$thisGroup,$count
)
$theseGroups = @()
if($count -eq $script:depthLimit) {
#Write-Host "Max depth reached at $thisGroup"
if (-Not $script:depthGroupsm.Contains($thisGroup)){
$script:depthGroupsm.Add($thisGroup,1)
}
return $thisGroup
}
if($thisGroup.Contains($FD)) {
Write-Host "foreign domain group $thisGroup"
return $theseGroups
}
try {
$members = Get-ADGroupMember $thisGroup
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
#write-host "bad group $thisGroup"
if (-Not $script:errorGrpsp.Contains($thisGroup)){
$script:errorGrpsp.Add($thisGroup,1)
}
return $theseGroups
}
catch {
# We have likely exceeded the 5k limit and must do this manually
$members = @()
$x = Get-ADGroup -Identity $thisGroup -Properties "Members"
foreach ($i in $x["Members"]) {
$members += Get-ADObject -Identity ($i)
}
}
$count += 1
foreach ($member in $members) {
if ($member.ObjectClass -eq "group") {
if($member.distinguishedName.Contains($FD)) {
continue
}
$theseGroups += $member.DistinguishedName
$theseGroups += getGroups $member.DistinguishedName $count
}
}
return $theseGroups
}
foreach ($group in $groups) {
$groupDN = @{}
$count = 0
$groupDN.Add($group.DistinguishedName,1)
Write-Host "Processing - $group"
$allSubGrps = getGroups $group.DistinguishedName 0
if($allSubGrps.Count) {
for ($i = 0; $i -lt $maxZgroups; $i++) {
try {
$group | Add-Member -MemberType NoteProperty -Name "Zgroup $i" -Value "" -Force
}
catch {
continue
}
}
foreach ($subGrp in $allSubGrps) {
try {
$groupDN.Add($subGrp,1)
}
catch {
continue
}
try {
$thisGroup = Get-ADGroup -Identity $subGrp -Properties "Members"
}
catch {
if (-Not $script:errorGrpsp.Contains($subGrp)){
$script:errorGrpsp.Add($subGrp,1)
}
continue
}
if ($thisGroup["Members"].Contains($group.DistinguishedName)) {
$group | Add-Member -MemberType NoteProperty -Name "Zgroup $count" -Value $subGrp -Force
$count += 1
}
}
}
# Keeps a running tab of findings in case of failure or manual stop
if($count) {
$dupGroups += $group
$dupGroups | Export-Csv -Path "cyclical-groups.csv" -NoTypeInformat -Force
}
if ($script:errorGrpsp.Count) {
$script:errorGrpsp.Keys | Out-File -FilePath "error-groups.txt" -Force
}
if ($script:depthGroupsm.Count) {
$script:depthGroupsm.Keys | Out-File -FilePath "max-depth-groups.txt" -Force
}
}