-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-password.ps1
232 lines (220 loc) · 9.46 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
#Requires -Version 5.1
<#
_ _ _____ _
| \ | | | __ \ | |
| \| | ___ __ __ ______ | |__) |__ _ ___ ___ __ __ ___ _ __ __| |
| . ` | / _ \\ \ /\ / /|______|| ___// _` |/ __|/ __|\ \ /\ / // _ \ | '__|/ _` |
| |\ || __/ \ V V / | | | (_| |\__ \\__ \ \ V V /| (_) || | | (_| |
|_| \_| \___| \_/\_/ |_| \__,_||___/|___/ \_/\_/ \___/ |_| \__,_|
#>
# -------- HELP --------
<#
.Credit
ALL credit goes to MAGC.
Code commented and documented by JVM
.Synopsis
This script will generate a new secure password string or credentialobject
.PARAMETER AsString
Specify if return object should be plaintext string
.PARAMETER Length
Specifies the length of the
.PARAMETER ForbiddenChars
Allows user to make specific chars forbiden
.PARAMETER MinLowerCaseChars
Set minimum amount of required lower case chars
.PARAMETER MinUpperCaseChars
Set minimum amount of upper case chars
.PARAMETER MinDigits
Set minimum amount of digits
.PARAMETER MinSpecialChars
Set minimum amount of special chars required
#>
function New-Password
{
[CmdletBinding(PositionalBinding = $false)]
[Alias("np")]
[OutputType([securestring],[string])]
#--------------------------------------------| PARAMETERS |--------------------------------------------#
Param
(
[Parameter()]
[switch]
$AsString,
[Parameter()]
[ValidateRange(8,[int]::MaxValue)]
[Int]
$Length=40,
[Parameter()]
[Alias("DisallowedChars")]
[ArgumentCompleter(
{
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
switch ($wordToComplete -replace "`"|'")
{
{"Lowercase" -like "$_*"}
{
[System.Management.Automation.CompletionResult]::new(
"abcdefghijklmnopqrstuvwxyz".ToCharArray().ForEach({"'$_'"}) -join ',',
'Lowercase',
[System.Management.Automation.CompletionResultType]::ParameterValue,
'Lowercase'
)
}
{"Uppercase" -like "$_*"}
{
[System.Management.Automation.CompletionResult]::new(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray().ForEach({"'$_'"}) -join ',',
'Uppercase',
[System.Management.Automation.CompletionResultType]::ParameterValue,
'Uppercase'
)
}
{"Digits" -like "$_*"}
{
[System.Management.Automation.CompletionResult]::new(
"1234567890".ToCharArray().ForEach({"'$_'"}) -join ',',
'Digits',
[System.Management.Automation.CompletionResultType]::ParameterValue,
'Digits'
)
}
{"Special" -like "$_*"}
{
[System.Management.Automation.CompletionResult]::new(
'/*!\"$%()=?{[]}+#-.,<_:;>~|@'.ToCharArray().ForEach({"'$_'"}) -join ',',
'Special',
[System.Management.Automation.CompletionResultType]::ParameterValue,
'Special'
)
}
{"Ambiguous" -like "$_*"}
{
[System.Management.Automation.CompletionResult]::new(
"IlOo0".ToCharArray().ForEach({"'$_'"}) -join ',',
'Ambiguous',
[System.Management.Automation.CompletionResultType]::ParameterValue,
'Ambiguous'
)
}
}
}
)]
[char[]]
$ForbiddenChars,
[Parameter()]
[ValidateRange(0,[int]::MaxValue)]
[Int]
$MinLowercaseChars=2,
[Parameter()]
[ValidateRange(0,[int]::MaxValue)]
[Int]
$MinUppercaseChars=2,
[Parameter()]
[ValidateRange(0,[int]::MaxValue)]
[Int]
$MinDigits=2,
[Parameter()]
[ValidateRange(0,[int]::MaxValue)]
[Int]
$MinSpecialChars=2
)
#---------------------------------------------| CHECK INPUT |--------------------------------------------#
begin
{
# Start out by building $AllAllowedChars variable. This is all subvariables concatinated, where no forbidden chars are included
[char[]]$AllAllowedChars = @(
([char[]]$AllowedLowercase = "abcdefghijklmnopqrstuvwxyz".ToCharArray().Where({$_ -cnotin $ForbiddenChars}))
([char[]]$AllowedUppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray().Where({$_ -cnotin $ForbiddenChars}))
([char[]]$AllowedDigits = "1234567890".ToCharArray().Where({$_ -notin $ForbiddenChars}))
([char[]]$AllowedSpecial = '/*!\"$%()=?{[]}+#-.,<_:;>~|@'.ToCharArray().Where({$_ -notin $ForbiddenChars}))
)
# FillerCharCount refers to the amount of characters not dictated by the required minimum of each type
[int]$FillerCharCount = $Length - ($MinLowercaseChars + $MinUppercaseChars + $MinDigits + $MinSpecialChars)
# For all if statements below, throw erorr if minimum requirements not met.
if ($FillerCharCount -lt 0)
{
throw "The specified length is less than the sum of the minimum character counts."
}
if ($AllowedLowercase.Count -lt 1 -and $MinLowercaseChars -gt 0)
{
throw "There are not enough allowed lowercase chars for the specified minimum lowercase count."
}
if ($AllowedUppercase.Count -lt 1 -and $MinUppercaseChars -gt 0)
{
throw "There are not enough allowed uppercase chars for the specified minimum uppercase count."
}
if ($AllowedDigits.Count -lt 1 -and $MinDigits -gt 0)
{
throw "There are not enough allowed digits for the specified minimum digit count."
}
if ($AllowedSpecial.Count -lt 1 -and $MinSpecialChars -gt 0)
{
throw "There are not enough allowed special chars for the specified minimum special count."
}
# Function to generate random chars for array. Takes the chararray to populate and an amount as input
function GetRandomChars ([char[]]$CharArray, [int]$Amount)
{
# Check if input is valid
if ($CharArray.Count -gt 0 -and $Amount -gt 0)
{
# Fills array with random chars from input array
for ($i = 0; $i -lt $Amount; $i++)
{
$CharArray[(Get-Random -Maximum $CharArray.Count)]
}
}
}
}
#------------------------------------------| BUILD PASSWORD |------------------------------------------#
process
{
try
{
if ($AsString)
{
# User wants output as plain text string
$StringBuilder = [System.Text.StringBuilder]::new($Length)
}
else
{
# User want output as secure string
$SecureString = [securestring]::new()
}
# Get all random chars in fixed position with GetRandomChars function
# Randomize their order with Get-Random,
# for each char either append to secure string or plain text string depending on user choice
@(
GetRandomChars -CharArray $AllowedLowercase -Amount $MinLowercaseChars
GetRandomChars -CharArray $AllowedUppercase -Amount $MinUppercaseChars
GetRandomChars -CharArray $AllowedDigits -Amount $MinDigits
GetRandomChars -CharArray $AllowedSpecial -Amount $MinSpecialChars
GetRandomChars -CharArray $AllAllowedChars -Amount $FillerCharCount
) |
Get-Random -Count $Length | ForEach-Object -Process {
if ($AsString)
{
$null = $StringBuilder.Append($_)
}
else
{
$SecureString.AppendChar($_)
}
}
# Entire pass wword has been built
if ($AsString)
{
# Return plaintext string if user asks for that
$StringBuilder.ToString()
}
else
{
# return secure string if user did not ask for cleartext
$SecureString
}
}
catch
{
Write-Error $_
}
}
}