-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathAddRandomToGroups.ps1
169 lines (142 loc) · 5.32 KB
/
AddRandomToGroups.ps1
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
Function AddRandomToGroups {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $false,
Position = 1,
HelpMessage = 'Supply a result from get-addomain')]
[Object[]]$Domain,
[Parameter(Mandatory = $false,
Position = 2,
HelpMessage = 'Supply a result from get-aduser -filter *')]
[Object[]]$UserList,
[Parameter(Mandatory = $false,
Position = 3,
HelpMessage = 'Supply a result from Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -eq "Global" } -Properties isCriticalSystemObject')]
[Object[]]$GroupList,
[Parameter(Mandatory = $false,
Position = 4,
HelpMessage = 'Supply a result from Get-ADGroup -Filter { GroupScope -eq "domainlocal" } -Properties isCriticalSystemObject')]
[Object[]]$LocalGroupList,
[Parameter(Mandatory = $false,
Position = 5,
HelpMessage = 'Supply a result from Get-ADComputer -f *')]
[Object[]]$CompList
)
##BEGIN STUFF
if(!$PSBoundParameters.ContainsKey('Domain')){
$dom = get-addomain
$setDC = $dom.pdcemulator
$dnsroot = $dom.dnsroot
$dn = $dom.distinguishedname
}
else {
$setDC = $Domain.pdcemulator
$dnsroot = $Domain.dnsroot
}
if (!$PSBoundParameters.ContainsKey('UserList')){
$allUsers = get-aduser -Filter *
}else {
$allUsers = $UserList
}
if (!$PSBoundParameters.ContainsKey('GroupList')){
$allGroups = Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -eq "Global" } -Properties isCriticalSystemObject
}else {
$allGroups = $GroupList
}
if (!$PSBoundParameters.ContainsKey('LocalGroupList')){
$allGroupsLocal = Get-ADGroup -Filter { GroupScope -eq "domainlocal" } -Properties isCriticalSystemObject
}else {
$allGroupsLocal = $LocalGroupList
}
if (!$PSBoundParameters.ContainsKey('CompList')){
$allcomps = Get-ADComputer -f *
}else {
$allcomps = $CompList
}
cd ad:
<#Pick X number of random users#>
$UsersInGroupCount = [math]::Round($allusers.count * .8) #need to round to int. need to check this works
$GroupsInGroupCount = [math]::Round($allGroups.count * .2)
$CompsInGroupCount = [math]::Round($allcomps.count * .1)
<#
$groupsall = Get-ADGroup -Filter { GroupCategory -eq "Security" -and GroupScope -eq "Global" } -Properties isCriticalSystemObject
PS \BadBlood> $groupsall.Count
1960
PS \BadBlood> $groupsall|where-object -Property iscriticalsystemobject -eq $true
#>
#get user list
$AddUserstoGroups = get-random -count $UsersInGroupCount -InputObject $allUsers
$allGroupsFiltered = $allGroups|where-object -Property iscriticalsystemobject -ne $true
#add a large number of users to a large number of non critical groups
Foreach ($user in $AddUserstoGroups){
#get how many groups
$num = 1..10|Get-Random
$n = 0
do{
$randogroup = $allGroupsFiltered|Get-Random
#add to group
try{Add-ADGroupMember -Identity $randogroup -Members $user}
catch{}
$n++
}while($n -le $num)
}
#add a few people to a small number of critical groups
$allGroupsCrit = $allGroups|where-object -Property iscriticalsystemobject -eq $true|Where-Object -Property name -ne "Domain Users" | Where-Object -Property name -ne "Domain Guests"
$allGroupsCrit|%{
$num = 2..5|Get-Random
try{Add-ADGroupMember -Identity $_ -Members (get-random -count $num -InputObject $allUsers)}
catch{}
}
#add a few people to a small number of critical local groups
$allGroupsLocal|%{
$num = 1..3|Get-Random
try{Add-ADGroupMember -Identity $_ -Members (get-random -count $num -InputObject $allUsers)}
catch{}
}
#$AddUserstoGroups = get-random -count (2..8|get-random) -InputObject $allUsers
#do nesting for all groups
#add a large number of users to a large number of non critical groups
#source is the input obj allGroupsFiltered, so i'm basically adding allgroupsfiltered to random non significant groups in AD.
#this is like adding domain admins to 'iis server 1 admins' or 'pwd reset' groups
$AddGroupstoGroups = get-random -count $GroupsInGroupCount -InputObject $allGroupsFiltered
Foreach ($group in $AddGroupstoGroups){
#get how many groups
$num = 1..2|Get-Random
$n = 0
do{
$randogroup = $allGroupsFiltered|Get-Random
#add to group
try{Add-ADGroupMember -Identity $randogroup -Members $group}
catch{}
$n++
}while($n -le $num)
}
# add all critical groups to 2-5 other random groups
$allGroupsCrit|%{
#get how many groups
$num = 1..3|Get-Random
$n = 0
do{
$randogroup = $allGroupsFiltered|Get-Random
#add to group
try{Add-ADGroupMember -Identity $randogroup -Members $_}
catch{}
$n++
}while($n -le $num)
}
$addcompstoGroups = @()
$addcompstogroups = get-random -count $compsInGroupCount -InputObject $allcomps
Foreach ($comp in $addcompstogroups){
#get how many groups
$num = 1..5|Get-Random
$n = 0
do{
$randogroup = $allGroupsFiltered|Get-Random
#add to group
try{Add-ADGroupMember -Identity $randogroup -Members $comp}
catch{}
$n++
}while($n -le $num)
}
}