-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-Password.ps1
431 lines (368 loc) · 15.6 KB
/
New-Password.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
Function New-Password {
<#
.SYNOPSIS
Generates a random password.
.DESCRIPTION
The New-Password cmdlet generates a string of random characters, generally used to create a password. You can specify the pools of characters to generate from, supply your own, or use a template.
.PARAMETER Length
Specifies the number of characters for the generated password.
.PARAMETER Mode
Specifies whether to generate the password evenly between the different alphabets as the password is generated, or to generate the password completely at random (characters from larger alphabets are more likely to end up getting picked). To illustrate the difference better, call the cmdlet with the -Verbose parameter.
Valid values are:
EqualMode: Distributes the password amongst the selected alphabets equally. This is the default.
TotalMode: Selects from the entire alphabet space (larger alphabets will take up a bigger portion of the password).
.PARAMETER UseLowerCase
Specifies whether or not to use lowercase letters in the password pool.
.PARAMETER UseUpperCase
Specifies whether or not to use uppercase letters in the password pool.
.PARAMETER UseDecimalNumbers
Specifies whether or not to use decimal numbers in the password pool.
.PARAMETER UseSymbols
Specifies whether or not to use symbols in the password pool.
.PARAMETER AvoidSimilar
Specifies whether or not to avoid characters that looke alike, and can be hard to discern from one another.
.PARAMETER AvoidProgramming
Specifies whether or not to avoid characters that are used in programming languages.
.PARAMETER PhoneticOutput
Prints the password as a series of words from the phonetic alphabet.
.PARAMETER CustomPattern
Specifies a custom set of characters that should be used as an alphabet.
.PARAMETER Template
Generate password based on a template string. The following placeholders are accepted:
l: Letters
v: Vowels
c: Consonants
!: Symbols
d: Decimal numbers
h: Hexadecimal numbers
o: Octal numbers
b: Binary numbers
a: Letters and decimal numbers
*: Letters, decimal numbers and symbols
.: Lowercase modifier
:: Uppercase modifier
-: Used for escaping any of the above placeholders
See examples for more information.
.EXAMPLE
Generate a random password consisting of 15 characters, selected from lowercase letters, uppercase letters and numbers.
New-Password
.EXAMPLE
Generate a 20-digit random binary number.
"01" | New-Password -Length 20
or
New-Password -Template 'bbbbbbbbbbbbbbbbbbbb'
.EXAMPLE
Generate a password that includes symbols, numbers and lowercase letters.
New-Password -UseSymbols -UseUpperCase:$false
.EXAMPLE
Generate a password where the characters are evenly distributed amongst all the alphabets.
New-Password -UseSymbols -Mode EqualMode
.EXAMPLE
Generate a password and print it with the phonetic alphabet to make it easier to remember / communicate.
New-Password -PhoneticOutput
.EXAMPLE
Generate a typical Azure-type password
New-Password -Template ':c.v.c.v.cdd'
.EXAMPLE
Generate a password where some of the characters are predefined. This will generate a password with three letters, an underscore, a colon, a symbol, another colon, another underscore, and finally three more letters.
New-Password -Template 'lll_-:!-:_lll'
.INPUTS
System.String
.OUTPUTS
System.String
#>
[CmdletBinding(DefaultParameterSetName='StandardSet')]
Param (
[Parameter(ParameterSetName='StandardSet')]
[Parameter(ParameterSetName='CustomPatternSet')]
[Int]
$Length = 15,
# Use -Verbose to better understand the difference between these two
[Parameter(ParameterSetName='StandardSet')]
[ValidateSet ("TotalMode", "EqualMode")]
[String]
$Mode='EqualMode',
[Parameter(ParameterSetName='TemplateSet', Mandatory=$true)]
[String]
$Template = '',
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$UseLowerCase=$true,
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$UseUpperCase=$true,
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$UseDecimalNumbers=$true,
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$UseSymbols,
[Parameter(ParameterSetName='TemplateSet')]
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$AvoidSimilar,
[Parameter(ParameterSetName='TemplateSet')]
[Parameter(ParameterSetName='StandardSet')]
[Switch]
$AvoidProgramming,
[Parameter(ParameterSetName='StandardSet')]
[Parameter(ParameterSetName='TemplateSet')]
[Parameter(ParameterSetName='CustomPatternSet')]
[Switch]
$PhoneticOutput,
[Parameter(ValueFromPipeline=$true, ParameterSetName='CustomPatternSet', Mandatory=$true)]
[String]
$CustomPattern
)
#region Declarations
$SimilarCharacters = @( '1', '|', 'I', 'l', '!', 'O', '0', '`', '´', "'", ';', ':' )
$ProgrammingCharacters = @( '$', "'", '"', '&', ',', '?', '@', '#', '<', '>', '(', ')', '{', '}', '[', ']', '/', '\' )
# Statistical counters
$LCC = 0
$UCC = 0
$NC = 0
$SC = 0
# TODO: Replace these with some sort of auto-generated list of letters, based on i18n, etc
$LowerCaseAlphabet = @( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" )
$UpperCaseAlphabet = @( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" )
$Vowels = @( 'a', 'e', 'i', 'o', 'u', 'y' )
$Consonants = @( "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z" )
$DecimalAlphabet = @( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" )
$HexaDecimalAlphabet = @( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' )
$OctalAlphabet = @( '0', '1', '2', '3', '4', '5', '6', '7' )
$BinaryAlphabet = @( '0', '1' )
$SymbolAlphabet = @( "§", "|", "!", "`"", "@", "#", "£", "¤", "$", "%", "€", "&", "/", "{", "(", "[", ")", "]", "=", "}", "\", "``", "´", "¨", "^", "~", "'", "*", "<", ">", ",", ";", ".", ":", "-", "_" )
$TotalAlphabet = @()
#endregion
Function ConvertTo-Phonetic ([String] $Character) {
$PhoneticTable = @{ 'a' = 'alfa'; 'b' = 'bravo'; 'c' = 'charlie'; 'd' = 'delta'; 'e' = 'echo'; 'f' = 'foxtrot'; 'g' = 'golf'; 'h' = 'hotel'; 'i' = 'india'; 'j' = 'juliett'; 'k' = 'kilo'; 'l' = 'lima'; 'm' = 'mike'; 'n' = 'november'; 'o' = 'oscar'; 'p' = 'papa'; 'q' = 'quebec'; 'r' = 'romeo'; 's' = 'sierra'; 't' = 'tango'; 'u' = 'uniform'; 'v' = 'victor'; 'w' = 'whiskey'; 'x' = 'x-ray'; 'y' = 'yankee'; 'z' = 'zulu' }
if ($LowerCaseAlphabet.Contains($Character.ToLower())) {
$retval = $null
if ($Character.ToLower() -ceq $Character) {
$retval = $PhoneticTable[$Character]
} else {
$retval = ($PhoneticTable[$Character.ToLower()]).ToUpper()
}
return $retval
} else {
return $Character
}
}
Function Approve ([String] $Character) {
if (!($AvoidSimilar -or $AvoidProgramming)) {
return $true
} else {
if ($AvoidSimilar -and $SimilarCharacters.Contains($Character)) {
return $false
}
if ($AvoidProgramming -and $ProgrammingCharacters.Contains($Character)) {
return $false
}
}
return $true
}
Function Count ([String] $Character) {
if ($LowerCaseAlphabet.Contains($Character)) {
$SV = Get-Variable -Name LCC -Scope 1
$SV.Value = $SV.Value + 1
return
}
if ($UpperCaseAlphabet.Contains($Character)) {
$SV = Get-Variable -Name UCC -Scope 1
$SV.Value = $SV.Value + 1
return
}
if ($DecimalAlphabet.Contains($Character)) {
$SV = Get-Variable -Name NC -Scope 1
$SV.Value = $SV.Value + 1
return
}
if ($SymbolAlphabet.Contains($Character)) {
$SV = Get-Variable -Name SC -Scope 1
$SV.Value = $SV.Value + 1
return
}
return
}
Function Get-Case ([String] $Character) {
if ($Character.ToLower() -ceq $Character) {
return $true
} else {
return $false
}
}
Function Get-Pool ([String] $CharacterClass) {
switch ($CharacterClass) {
'l' { return $LowerCaseAlphabet; break }
'v' { return $Vowels; break }
'c' { return $Consonants; break }
'h' { return $HexaDecimalAlphabet; break }
'a' { return $LowerCaseAlphabet + $DecimalAlphabet; break }
'*' { return $LowerCaseAlphabet + $DecimalAlphabet + $SymbolAlphabet; break }
'd' { return $DecimalAlphabet; break }
'o' { return $OctalAlphabet; break }
'b' { return $BinaryAlphabet; break }
'!' { return $SymbolAlphabet; break }
default { return $CharacterClass; break }
}
}
Function Set-RandomCase ([String] $Character) {
$Result = 0..1 | Get-SecureRandom
if ($Result -eq 0) {
return $Character.ToLower()
} else {
return $Character.ToUpper()
}
}
Function Get-TemplatePassword ([String] $TemplateString) {
$TemplatePassword = ''
$AllGood = $true
for ($c = 0; $c -lt $TemplateString.Length; $c++) {
switch ($TemplateString[$c]) {
'-' {
if ($TemplateString.Length - 1 -ne $c) {
$TemplatePassword += $TemplateString[$c + 1]
$c += 1
} else {
Write-Error 'Syntax error'
$AllGood = $false
break
}
}
'.' {
if ($TemplateString.Length - 1 -ne $c) {
$Pool = Get-Pool $TemplateString[$c + 1]
do {
$PasswordChar = ($Pool | Get-SecureRandom).ToLower()
} while (!(Approve $PasswordChar))
$TemplatePassword += $PasswordChar
$c += 1
} else {
Write-Error 'Syntax error'
$AllGood = $false
break
}
}
':' {
if ($TemplateString.Length - 1 -ne $c) {
$Pool = Get-Pool $TemplateString[$c + 1]
do {
$PasswordChar = ($Pool | Get-SecureRandom).ToUpper()
} while (!(Approve $PasswordChar))
$TemplatePassword += $PasswordChar
$c += 1
} else {
Write-Error 'Syntax error'
$AllGood = $false
break
}
}
{'*', 'a', 'l', 'c', 'v', 'd', '!', 'h', 'b', 'o' -ccontains $_} {
$Pool = Get-Pool $TemplateString[$c]
do {
$PasswordChar = Set-RandomCase ($Pool | Get-SecureRandom)
} while (!(Approve $PasswordChar))
$TemplatePassword += $PasswordChar
}
default { $TemplatePassword += $TemplateString[$c] }
}
}
if ($AllGood) {
return $TemplatePassword
} else {
return $null
}
}
$Password = ""
if ($Template -eq '') {
$Variations = 0
if ($CustomPattern -ne '') {
$Variations++
foreach ($letter in $CustomPattern.GetEnumerator()) {
if (!$TotalAlphabet.Contains($letter)) {
$TotalAlphabet += $letter
}
}
} else {
if ($Mode -eq "TotalMode") {
if ($UseLowerCase) {
$Variations++
$TotalAlphabet += $LowerCaseAlphabet
}
if ($UseUpperCase) {
$Variations++
$TotalAlphabet += $UpperCaseAlphabet
}
if ($UseDecimalNumbers) {
$Variations++
$TotalAlphabet += $DecimalAlphabet
}
if ($UseSymbols) {
$Variations++
$TotalAlphabet += $SymbolAlphabet
}
} else {
if ($UseLowerCase) {
$Variations++
$TotalAlphabet += ,$LowerCaseAlphabet
}
if ($UseUpperCase) {
$Variations++
$TotalAlphabet += ,$UpperCaseAlphabet
}
if ($UseDecimalNumbers) {
$Variations++
$TotalAlphabet += ,$DecimalAlphabet
}
if ($UseSymbols) {
$Variations++
$TotalAlphabet += ,$SymbolAlphabet
}
}
}
if ($Variations -gt 0) {
if ($Mode -eq "TotalMode" -or $CustomPattern -ne "") {
for ($i = 0; $i -lt $Length;$i++) {
$index = Get-SecureRandom -Minimum 0 -Maximum $TotalAlphabet.Length
while (!(Approve $TotalAlphabet[$index])) {
$index = Get-SecureRandom -Minimum 0 -Maximum $TotalAlphabet.Length
}
Count $TotalAlphabet[$index]
$Password = $Password + $TotalAlphabet[$index]
}
} else {
for ($i = 0; $i -lt $Length;$i++) {
$variation = $null
if ($Variations -gt 1) {
$variation = Get-SecureRandom -Minimum 0 -Maximum $Variations
} else {
$variation = 0
}
$index = Get-SecureRandom -Minimum 0 -Maximum $TotalAlphabet[$variation].Length
while (!(Approve $TotalAlphabet[$variation][$index])) {
$index = Get-SecureRandom -Minimum 0 -Maximum $TotalAlphabet[$variation].Length
}
Count $TotalAlphabet[$variation][$index]
$Password += $TotalAlphabet[$variation][$index]
}
}
Write-Verbose ("LC:{0:P} UC:{1:P} NC:{2:P} SC:{3:P}" -f ($LCC / $Length), ($UCC / $Length), ($NC / $Length), ($SC / $Length))
} else {
Write-Error -Message "Invalid combination of parameters"
return
}
} else {
$Password = Get-TemplatePassword $Template
}
if ($PhoneticOutput) {
$PhoneticPassword = ""
for ($i = 0; $i -lt $Password.Length; $i++) {
$PhoneticPassword += ConvertTo-Phonetic $Password[$i]
$PhoneticPassword += ' '
}
$PhoneticPassword = $PhoneticPassword.TrimEnd()
Write-Output $PhoneticPassword
} else {
Write-Output $Password
}
}