-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathCreateUsers.ps1
325 lines (256 loc) · 12.3 KB
/
CreateUsers.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
Function CreateUser{
<#
.SYNOPSIS
Creates a user in an active directory environment based on random data
.DESCRIPTION
Starting with the root container this tool randomly places users in the domain.
.PARAMETER Domain
The stored value of get-addomain is used for this. It is used to call the PDC and other items in the domain
.PARAMETER OUList
The stored value of get-adorganizationalunit -filter *. This is used to place users in random locations.
.PARAMETER ScriptDir
The location of the script. Pulling this into a parameter to attempt to speed up processing.
.EXAMPLE
.NOTES
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author's blog: https://www.secframe.com
#>
[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-adorganizationalunit -filter *')]
[Object[]]$OUList,
[Parameter(Mandatory = $false,
Position = 3,
HelpMessage = 'Supply the script directory for where this script is stored')]
[string]$ScriptDir
)
if(!$PSBoundParameters.ContainsKey('Domain')){
if($args[0]){
$setDC = $args[0].pdcemulator
$dnsroot = $args[0].dnsroot
}
else{
$setDC = (Get-ADDomain).pdcemulator
$dnsroot = (get-addomain).dnsroot
}
}
else {
$setDC = $Domain.pdcemulator
$dnsroot = $Domain.dnsroot
}
if (!$PSBoundParameters.ContainsKey('OUList')){
if($args[1]){
$OUsAll = $args[1]
}
else{
$OUsAll = get-adobject -Filter {objectclass -eq 'organizationalunit'} -ResultSetSize 300
}
}else {
$OUsAll = $OUList
}
if (!$PSBoundParameters.ContainsKey('ScriptDir')){
if($args[2]){
# write-host "line 70"
$scriptPath = $args[2]}
else{
# write-host "did i get here"
$scriptPath = "$((Get-Location).path)\AD_Users_Create\"
}
}else{
$scriptpath = $ScriptDir
}
function New-SWRandomPassword {
<#
.Synopsis
Generates one or more complex passwords designed to fulfill the requirements for Active Directory
.DESCRIPTION
Generates one or more complex passwords designed to fulfill the requirements for Active Directory
.EXAMPLE
New-SWRandomPassword
C&3SX6Kn
Will generate one password with a length between 8 and 12 chars.
.EXAMPLE
New-SWRandomPassword -MinPasswordLength 8 -MaxPasswordLength 12 -Count 4
7d&5cnaB
!Bh776T"Fw
9"C"RxKcY
%mtM7#9LQ9h
Will generate four passwords, each with a length of between 8 and 12 chars.
.EXAMPLE
New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4
3ABa
Generates a password with a length of 4 containing atleast one char from each InputString
.EXAMPLE
New-SWRandomPassword -InputStrings abc, ABC, 123 -PasswordLength 4 -FirstChar abcdefghijkmnpqrstuvwxyzABCEFGHJKLMNPQRSTUVWXYZ
3ABa
Generates a password with a length of 4 containing atleast one char from each InputString that will start with a letter from
the string specified with the parameter FirstChar
.OUTPUTS
[String]
.NOTES
Written by Simon WÃ¥hlin, blog.simonw.se
I take no responsibility for any issues caused by this script.
.FUNCTIONALITY
Generates random passwords
.LINK
http://blog.simonw.se/powershell-generating-random-password-for-active-directory/
#>
[CmdletBinding(DefaultParameterSetName='FixedLength',ConfirmImpact='None')]
[OutputType([String])]
Param
(
# Specifies minimum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({$_ -gt 0})]
[Alias('Min')]
[int]$MinPasswordLength = 12,
# Specifies maximum password length
[Parameter(Mandatory=$false,
ParameterSetName='RandomLength')]
[ValidateScript({
if($_ -ge $MinPasswordLength){$true}
else{Throw 'Max value cannot be lesser than min value.'}})]
[Alias('Max')]
[int]$MaxPasswordLength = 20,
# Specifies a fixed password length
[Parameter(Mandatory=$false,
ParameterSetName='FixedLength')]
[ValidateRange(1,2147483647)]
[int]$PasswordLength = 8,
# Specifies an array of strings containing charactergroups from which the password will be generated.
# At least one char from each group (string) will be used.
[String[]]$InputStrings = @('abcdefghijkmnpqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!#%&'),
# Specifies a string containing a character group from which the first character in the password will be generated.
# Useful for systems which requires first char in password to be alphabetic.
[String] $FirstChar,
# Specifies number of passwords to generate.
[ValidateRange(1,2147483647)]
[int]$Count = 1
)
Begin {
Function Get-Seed{
# Generate a seed for randomization
$RandomBytes = New-Object -TypeName 'System.Byte[]' 4
$Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider'
$Random.GetBytes($RandomBytes)
[BitConverter]::ToUInt32($RandomBytes, 0)
}
}
Process {
For($iteration = 1;$iteration -le $Count; $iteration++){
$Password = @{}
# Create char arrays containing groups of possible chars
[char[][]]$CharGroups = $InputStrings
# Create char array containing all chars
$AllChars = $CharGroups | ForEach-Object {[Char[]]$_}
# Set password length
if($PSCmdlet.ParameterSetName -eq 'RandomLength')
{
if($MinPasswordLength -eq $MaxPasswordLength) {
# If password length is set, use set length
$PasswordLength = $MinPasswordLength
}
else {
# Otherwise randomize password length
$PasswordLength = ((Get-Seed) % ($MaxPasswordLength + 1 - $MinPasswordLength)) + $MinPasswordLength
}
}
# If FirstChar is defined, randomize first char in password from that string.
if($PSBoundParameters.ContainsKey('FirstChar')){
$Password.Add(0,$FirstChar[((Get-Seed) % $FirstChar.Length)])
}
# Randomize one char from each group
Foreach($Group in $CharGroups) {
if($Password.Count -lt $PasswordLength) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$Group[((Get-Seed) % $Group.Count)])
}
}
# Fill out with chars from $AllChars
for($i=$Password.Count;$i -lt $PasswordLength;$i++) {
$Index = Get-Seed
While ($Password.ContainsKey($Index)){
$Index = Get-Seed
}
$Password.Add($Index,$AllChars[((Get-Seed) % $AllChars.Count)])
}
Write-Output -InputObject $(-join ($Password.GetEnumerator() | Sort-Object -Property Name | Select-Object -ExpandProperty Value))
}
}
}
#get owner all parameters and store as variable to call upon later
#=======================================================================
#will work on adding things to containers later $ousall += get-adobject -Filter {objectclass -eq 'container'} -ResultSetSize 300|where-object -Property objectclass -eq 'container'|where-object -Property distinguishedname -notlike "*}*"|where-object -Property distinguishedname -notlike "*DomainUpdates*"
$ouLocation = (Get-Random $OUsAll).distinguishedname
$accountType = 1..100|get-random
if($accountType -le 3){ # X percent chance of being a service account
#service
$nameSuffix = "SA"
$description = 'Created with secframe.com/badblood.'
#removing do while loop and making random number range longer, sorry if the account is there already
# this is so that I can attempt to import multithreading on user creation
$name = ""+ (Get-Random -Minimum 100 -Maximum 9999999999) + "$nameSuffix"
}else{
$surname = get-content("$($scriptpath)\Names\familynames-usa-top1000.txt")|get-random
# Write-Host $surname
$genderpreference = 0,1|get-random
if ($genderpreference -eq 0){$givenname = get-content("$($scriptpath)\Names\femalenames-usa-top1000.txt")|get-random}else{$givenname = get-content($scriptpath + '\Names\malenames-usa-top1000.txt')|get-random}
$name = $givenname+"_"+$surname
}
$departmentnumber = [convert]::ToInt32('9999999')
#Need to figure out how to do the L attribute
$description = 'Created with secframe.com/badblood.'
$pwd = New-SWRandomPassword -MinPasswordLength 22 -MaxPasswordLength 25
#======================================================================
#
$passwordinDesc = 1..1000|get-random
$pwd = New-SWRandomPassword -MinPasswordLength 22 -MaxPasswordLength 25
if ($passwordinDesc -lt 10) {
$description = 'Just so I dont forget my password is ' + $pwd
}else{}
if($name.length -gt 20){
$name = $name.substring(0,20)
}
$exists = $null
try {
$exists = Get-ADUSer $name -ErrorAction Stop
} catch{}
if($exists){
return $true
}
new-aduser -server $setdc -Description $Description -DisplayName $name -name $name -SamAccountName $name -Surname $name -Enabled $true -Path $ouLocation -AccountPassword (ConvertTo-SecureString ($pwd) -AsPlainText -force)
$pwd = ''
#==============================
# Set Does Not Require Pre-Auth for ASREP
#==============================
$setASREP = 1..1000|get-random
if($setASREP -lt 20){
Get-ADuser $name | Set-ADAccountControl -DoesNotRequirePreAuth:$true
}
#===============================
#SET ATTRIBUTES - no additional attributes set at this time besides UPN
#Todo: Set SPN for kerberoasting. Example attribute edit is in createcomputers.ps1
#===============================
$upn = $name + '@' + $dnsroot
try{Set-ADUser -Identity $name -UserPrincipalName "$upn" }
catch{}
# return $false
################################
#End Create User Objects
################################
}