Вот так всегда, только придумаешь идею, а она уже кем-то решена, при чём решена исчерпывающе
Осталось только доработать её для себя, так как издеваться над пользователями и давать им супер сложные пароли себе дороже.
Так скрипт выглядит в полном варианте
function RandomPassword ([int]$intPasswordLength)
{
if ($intPasswordLength -lt 4) {return "password cannot be <4 chars}
$strNumbers = "1234567890"
$strCapitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$strLowerLetters = "abcdefghjkmnpqrstuvwxyz"
$strSymbols = "!%^&*()+=/?{}[]~,.<>:"$rand = new-object random
for ($a=1; $a -le $intPasswordLength; $a++)
{
if ($a -gt 4)
{
$b = $rand.next(0,4) + $a
$b = $b % 4 + 1
} else { $b = $a }
switch ($b)
{
"1" {$b = "$strNumbers"}
"2" {$b = "$strCapitalLetters"}
"3" {$b = "$strLowerLetters"}
"4" {$b = "$strSymbols"}
}
$charset = $($b)
$number = $rand.next(0,$charset.Length)
$RandomPassword += $charset[$number]
}
return $RandomPassword
}
Мне осталось только убрать большие буквы и символы, а так же чтобы по-умолчанию скрипт генерил шестизначный пароль.
function RandomPassword ([int]$intPasswordLength)
{
if ($intPasswordLength -lt 4) {$intPasswordLength = 6}
$strNumbers = "1234567890"
$strCapitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$strLowerLetters = "abcdefghijklmnopqrstuvwxyz"
$strSymbols = "!%^&*()+=/?{}[]~,.<>:"
$rand = new-object randomfor ($a=1; $a -le $intPasswordLength; $a++)
{
# Цифру от 1 до 4
if ($a -gt 2)
{
$b = $rand.next(0,4) + $a
$b = $b % 2 + 1
} else { $b = $a }
switch ($b)
{
"1" {$b = "$strNumbers"}
"3" {$b = "$strCapitalLetters"}
"2" {$b = "$strLowerLetters"}
"4" {$b = "$strSymbols"}
}
$charset = $($b)
$number = $rand.next(0,$charset.Length)
$RandomPassword += $charset[$number]
}
return $RandomPassword
}
Оригинал скрипта лежит тут http://www.terminal23.net/2007/06/powershell_random_password_gen.html
