Last active
March 8, 2022 07:13
-
-
Save mhudasch/db6157bdd2d472eb087a9ccfd49df683 to your computer and use it in GitHub Desktop.
Creates Random strings like passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-RandomString { | |
param( | |
[Parameter()][ValidateRange(8, 4000)] | |
[int]$MaxLength = 16, | |
[Parameter()][ValidateRange(1,1000)] | |
[int]$MinCountLowerCaseLetters=1, | |
[Parameter()][ValidateRange(1,1000)] | |
[int]$MinCountUpperCaseLetters=1, | |
[Parameter()][ValidateRange(1,1000)] | |
[int]$MinCountDigits=1, | |
[Parameter()][ValidateRange(1,1000)] | |
[int]$MinCountSpecialLetters=1 | |
) | |
begin { | |
$lowerCaseLetters = [char[]]@('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z'); | |
$upperCaseLetters = [char[]]@('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z'); | |
$digits = [char[]]@('1','2','3','4','5','6','7','8','9','0'); | |
$specialReadableAsciiCharacters = [char[]]@('!', '"', "'", '#', '$', '%', '&', '*', '+', ',', '.', '/', ':', ';', '=', '?', '@', '{', '}', '[', ']'); | |
} | |
process { | |
$requiredLength = $MinCountLowerCaseLetters + $MinCountUpperCaseLetters + $MinCountDigits + $MinCountSpecialLetters; | |
if($MaxLength -lt $requiredLength) { | |
throw "The maximum length of the password must be at least satisfy the required occurrences of the character groups."; | |
} | |
$result = New-Object -TypeName char[] -ArgumentList $MaxLength; | |
$randomIndizes = 1..$MaxLength | ForEach-Object { [pscustomobject]@{ Idx=$_-1;O=(Get-Random -Maximum $MaxLength) } } | Sort-Object -Property "O" | Select-Object -ExpandProperty "Idx"; | |
$requiredSet = @(1..$MinCountLowerCaseLetters | ForEach-Object { $lowerCaseLetters[(Get-Random -Maximum $lowerCaseLetters.Length)].ToString() }); | |
$requiredSet += @(1..$MinCountUpperCaseLetters | ForEach-Object { $upperCaseLetters[(Get-Random -Maximum $upperCaseLetters.Length)].ToString() }); | |
$requiredSet += @(1..$MinCountDigits | ForEach-Object { $digits[(Get-Random -Maximum $digits.Count)].ToString() }); | |
$requiredSet += @(1..$MinCountSpecialLetters | ForEach-Object { $specialReadableAsciiCharacters[(Get-Random -Maximum $specialReadableAsciiCharacters.Length)].ToString() }); | |
for($i = 0; $i -lt $MaxLength; $i++) { | |
if($i -lt $requiredLength) { | |
$result[$randomIndizes[$i]] = $requiredSet[$i]; | |
continue; | |
} | |
# the max length is greater than required minimum | |
# so randomly select a character class and fill in the rest | |
switch((Get-Random -Maximum 4)) { | |
0 { $result[$randomIndizes[$i]] = $lowerCaseLetters[(Get-Random -Maximum $lowerCaseLetters.Length)] } | |
1 { $result[$randomIndizes[$i]] = $upperCaseLetters[(Get-Random -Maximum $upperCaseLetters.Length)] } | |
2 { $result[$randomIndizes[$i]] = $digits[(Get-Random -Maximum $digits.Count)] } | |
3 { $result[$randomIndizes[$i]] = $specialReadableAsciiCharacters[(Get-Random -Maximum $specialReadableAsciiCharacters.Length)] } | |
} | |
} | |
$(-join $result) | Write-Output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment