ランダムなパスワードを生成する
次の関数を実行するとパスワードをランダムに生成します。
関数定義
function CreatePassword(
[int]$Length=8,[switch]$NoSmall,[switch]$NoLarge)
{
$smalls = 'abcdefghijklmnopqrstuvwxyz'
$larges = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$digits = '0123456789'
$charList = $digits
if(-not $NoSmall){ $charList += $smalls }
if(-not $NoLarge){ $charList += $larges }
$random = New-Object Random
$builder= New-Object Text.StringBuilder
for($i=0; $i -lt $Length; $i++)
{
$index = $random.Next($charList.Length)
$c = $charList[$index]
$builder.Append($c) | Out-Null
}
$builder.ToString()
}
実行例
> CreatePassword -Length 16 -NoLarge
81o0v6a8hxgw6wno
>
パラメータの意味
-Length |
パスワードの文字数 |
-NoLarge |
大文字を使用しない |
-NoSmall |
小文字を使用しない |