FunctionApp/Modules/SessionHostReplacer/internal/functions/Get-RandomPassword.ps1 (24 lines of code) (raw):
function Get-RandomPassword {
#Link: https://gist.github.com/onlyann/00d9bb09d4b1338ffc88a213509a6caf
param(
[Parameter(Mandatory = $false)]
[ValidateRange(12, 256)]
[int]
$length = 14
)
$symbols = '!@#$%^&*'.ToCharArray()
$characterList = 'a'..'z' + 'A'..'Z' + '0'..'9' + $symbols
do {
$password = ""
for ($i = 0; $i -lt $length; $i++) {
$randomIndex = [System.Security.Cryptography.RandomNumberGenerator]::GetInt32(0, $characterList.Length)
$password += $characterList[$randomIndex]
}
[int]$hasLowerChar = $password -cmatch '[a-z]'
[int]$hasUpperChar = $password -cmatch '[A-Z]'
[int]$hasDigit = $password -match '[0-9]'
[int]$hasSymbol = $password.IndexOfAny($symbols) -ne -1
}
until (($hasLowerChar + $hasUpperChar + $hasDigit + $hasSymbol) -ge 3)
$password
}