@@ -42,3 +42,87 @@ function Get-oAuthToken {
42
42
$AuthContext = Invoke-RestMethod - Method Post - Uri $LoginURL / $TenantId / oauth2/ token?api- version= 1.0 - Body $Body
43
43
Return " $ ( $AuthContext.token_type ) $ ( $AuthContext.access_token ) "
44
44
}
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