-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenpass.ps1
68 lines (57 loc) · 2.16 KB
/
genpass.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
# Define parameters with support for short options
param (
[switch]$help, # -help parameter to show help message
[int]$l = 16, # -l parameter to specify length
[string]$o = "a" # -o parameter to specify character options
)
function Show-Help {
Write-Host "Usage:"
Write-Host "./genpass.ps1 -l <password length> -o <character types>"
Write-Host "Options:"
Write-Host " l : Lowercase letters"
Write-Host " u : Uppercase letters"
Write-Host " d : Digits"
Write-Host " s : Special characters"
Write-Host " a : All character types"
Write-Host "Example: ./genpass.ps1 -l 12 -o 'lds'"
}
function Generate-StrongPassword {
param (
[int]$length,
[string]$options
)
# Define character sets
$lowercase = 'abcdefghijklmnopqrstuvwxyz'
$uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$digits = '0123456789'
$specialChars = '!@#$%^&*()-_=+[]{}<>?'
# Build the selected character set based on user options
$selectedChars = ""
if ($options -match "l" -or $options -match "a") { $selectedChars += $lowercase }
if ($options -match "u" -or $options -match "a") { $selectedChars += $uppercase }
if ($options -match "d" -or $options -match "a") { $selectedChars += $digits }
if ($options -match "s" -or $options -match "a") { $selectedChars += $specialChars }
# Error handling: Ensure at least one valid character set is selected
if ($selectedChars.Length -eq 0) {
Write-Host "Error: Please select a valid character set."
Show-Help
return
}
# Generate the password from the selected character set
$password = -join (1..$length | ForEach-Object { $selectedChars[(Get-Random -Minimum 0 -Maximum $selectedChars.Length)] })
return $password
}
# Show help message if -help is provided
if ($help) {
Show-Help
return
}
# Main script section with error handling
if ($l -lt 1) {
Write-Host "Error: Password length cannot be less than 1."
Show-Help
return
}
# Generate the strong password and display it
$strongPassword = Generate-StrongPassword -length $l -options $o
Write-Host "Generated strong password: $strongPassword"