Skip to content

Commit 20ae165

Browse files
authored
Add Get-RandomPassword function (#3)
+semver:minor
1 parent a47c3bc commit 20ae165

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

PSF.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Description = 'PowerShell Functions Module'
6969
# NestedModules = @()
7070

7171
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72-
FunctionsToExport = 'Get-oAuthToken'
72+
FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword')
7373

7474
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
7575
CmdletsToExport = @()
@@ -78,7 +78,7 @@ CmdletsToExport = @()
7878
# VariablesToExport = @()
7979

8080
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
81-
AliasesToExport = @()
81+
AliasesToExport = @('gpw')
8282

8383
# DSC resources to export from this module
8484
# DscResourcesToExport = @()

PSF.psm1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,87 @@ function Get-oAuthToken {
4242
$AuthContext = Invoke-RestMethod -Method Post -Uri $LoginURL/$TenantId/oauth2/token?api-version=1.0 -Body $Body
4343
Return "$($AuthContext.token_type) $($AuthContext.access_token)"
4444
}
45+
46+
Function Get-RandomPassword {
47+
<#
48+
.SYNOPSIS
49+
Generates random passwords.
50+
.DESCRIPTION
51+
Function to generate password based on character sets. Enables choice of password length and complexity.
52+
.EXAMPLE
53+
C:\PS> Get-RandomPassword
54+
Generates a 20 character complex password and copies the result to your clipboard.
55+
.EXAMPLE
56+
C:\PS> Get-RandomPassword 16 uln
57+
Generates a 16 character password with Upper (u), Lower (l) and Numbers (n) and copies the result to your clipboard.
58+
.PARAMETER Length
59+
Length of password to be generated.
60+
Default: 20
61+
.PARAMETER CharSets
62+
Choose which character sets to use. Options are U (upper case), L (lower case), N (number) and S (special characters).
63+
Default: ULNS
64+
.PARAMETER Exclude
65+
Exclude one or more characters from the password.
66+
Example: Get-RandomPassword -Exclude '#$' will exclude the # and $ characters from the password.
67+
.OUTPUTS
68+
A generated password.
69+
.NOTES
70+
Version 1.1.0
71+
#>
72+
73+
Param(
74+
[Parameter(Mandatory = $false, HelpMessage = "PWLength", Position = 0)]
75+
[int] $Length = 20,
76+
[Parameter(Mandatory = $False, Position = 1)]
77+
[ValidateSet("SQL", "ULN", "UL", "UN", "LN")]
78+
[string] $InputValue,
79+
[Parameter(Mandatory = $False, Position = 2)]
80+
[Char[]] $Exclude
81+
)
82+
# Declare empty variables
83+
$Password = @()
84+
$AllNonExcludedCharacters = @()
85+
[Char[]]$CharSets = "ULNS"
86+
$SQLExcluded = '\',"'",'"','%','$','`',',',';','I','l','0','O','1'
87+
if (($InputValue -ne "") -and ($InputValue -ne "SQL")) {$CharSets = $InputValue}
88+
if ($InputValue -eq "SQL") {$Exclude = $SQLExcluded}
89+
# Create character arrays for U, L, N and S.
90+
$CharacterSetArray = @{
91+
U = [Char[]](65 .. 90)
92+
L = [Char[]](97 .. 122)
93+
N = [Char[]](48 .. 57)
94+
S = [Char[]](33 .. 46)
95+
}
96+
97+
# For each character set (U, L, N, S) sent to the function.
98+
$CharSets | ForEach-Object {
99+
$NonExcludedTokens = @()
100+
# For each character in the character set array.
101+
$NonExcludedTokens = $CharacterSetArray."$_" | ForEach-Object {
102+
# Check to see if the character currently being looped is an excluded character (requested by the user).
103+
If ($Exclude -cNotContains $_) {
104+
# Add this character to the NonExcludedTokens array if not an excluded character.
105+
$_
106+
}
107+
}
108+
# If NonExcludedTokens contains any characters.
109+
If ($NonExcludedTokens) {
110+
# Add the characters to the AllNonExcludedCharacters array.
111+
$AllNonExcludedCharacters += $NonExcludedTokens
112+
# Append a random character from this NonExcludedTekons array to the password array.
113+
$Password += $NonExcludedTokens | Get-Random
114+
}
115+
}
116+
117+
# Until the password array contains the same number of characters as the length parameter.
118+
While ($Password.Count -lt $Length) {
119+
# Add a random character from the AllNonExcludedCharacters array to the password array.
120+
$Password += $AllNonExcludedCharacters | Get-Random
121+
}
122+
123+
# Randomise the characters in the Password array and join them as a single line. Output the line to screen and copy to clipboard.
124+
($Password | Sort-Object {Get-Random}) -Join "" | Tee-Object -v Output
125+
$Output | Clip
126+
}
127+
128+
New-Alias -Name gpw -Value Get-RandomPassword

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy