-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIronScripter-Prelude-1.ps1
60 lines (53 loc) · 1.7 KB
/
IronScripter-Prelude-1.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
function New-Password
{
[CmdletBinding()]
param(
[int]$Length = 10,
[int]$NonAlpha = 4
)
Begin
{
# Warn if nonalpha greater then length
if($NonAlpha -gt $Length)
{Write-Warning -Message "NonAlpha characters are greater than the total length";break}
# Blank array list for creating password string
[System.Collections.ArrayList]$pswd = @()
# Blank array list for checking at least 1 capital letter
[System.Collections.ArrayList]$capLetters = @()
# Character sets used to build password
$nonAplhaColl = ([char[]]([char]33..[char]64) + ([char[]]([char]91..[char]96)))
$alphaColl = ([char[]]([char]65..[char]90)) + ([char[]]([char]97..[char]122))
}
Process
{
# Add nonAlpha chars
$count = 0
do
{
$pswd.Add($(Get-Random -InputObject $nonAplhaColl)) *>$null
$count++
}
until($count -eq $NonAlpha)
# Add alpha chars
$count = 0
do
{
$pswd.Add($(Get-Random -InputObject $alphaColl)) *>$null
$count++
}
until ($count -eq ($length - $NonAlpha))
# Confirm at least 1 capital letter
$pswd | foreach {if([char]::IsUpper($_)){$capLetters.Add($_) *> $null}}
if($capLetters.Count -eq 0)
{
# If no capital letters, capitalize first letter found in $pswd
$firstLetter = $($pswd -cmatch '[a-z]')[0].tostring()
$pswd = $pswd -replace $firstLetter,$firstLetter.toUpper()
}
}
End
{
# Randomize $pswd and output array into a single string
($pswd | Sort-Object {Get-Random}) -join ''
}
}