PowerShellでパスワードを生成するスクリプト

PowerShellでランダムなパスワードを生成するスクリプトおよび使い方を公開します。

本スクリプトにより直接的および間接的障害が生じても一切責任を負いません。
あらかじめご了承ください。
自己責任のもとで本資料をご利用ください。


スポンサーリンク

関連記事

ランダムなパスワードを生成するPowerShellスクリプト

PowerShellで作成した関数は以下の通りです。
filegenpwd.zip ダウンロード

function Generate-Password() {
  if (-Not(($args.length -eq 1) -Or ($args.length -eq 2))) {
    Write-Output "ERR: Invalid argment`nUsage:`n`tGenerate-Password <password length> [included minimum symbol length]"
    return
  }
  $b = $True
  if (($args[0]).GetType().Name -ne "Int32") {
    Write-Output ("ERR: '" + $args[0] + "' is invalid argment")
    $b = $False
  }
  if ($args.length -eq 2 -And ($args[1]).GetType().Name -ne "Int32") {
    Write-Output ("ERR: '" + $args[1] + "' is invalid argment")
    $b = $False
  }
  if ($b -eq $False) { return }
  if ($args.length -eq 2 -And ($args[0] -lt $args[1])) {
    Write-Output ("ERR: Symbol length is not less than or equal to the password length.")
    return
  }
  $password_length = $args[0]
  $symbol_length = 0
  if ($args.length -eq 2) {
    $symbol_length = $args[1]
  }
  Add-type -AssemblyName System.Web
  $gen_pwd = [System.Web.Security.Membership]::GeneratePassword($password_length, $symbol_length)
  Write-Output $gen_pwd
}

使い方

上記のスクリプトは関数になっています。
みなさんが作成されているスクリプトに追加してもいいですし、本スクリプトを読み込んでも構いません。
以下、本スクリプトをCopy&PasteでPowerShellコンソールに貼り付けて使用してみます。

構文

Generate-Password 生成桁数 [最小記号数]

説明 最小記号数は省略可能です。

本スクリプトは、[System.Web.Security.Membership]のGeneratePasswordメソッドを使用しています。
https://msdn.microsoft.com/ja-jp/library/system.web.security.membership.generatepassword(v=vs.110).aspx
''本メソッドの仕様として、第二引数の記号数は、生成されるパスワード文字列内に記号が含まれる最小数を指定します。
よって生成するパスワード内の記号数を指定しているわけではありません。''

  1. PowerShellコンソールに貼り付け
    01.png
     
  2. 実際に使ってみます。

以上、PowerShellでランダムなパスワードを生成するサンプルスクリプトでした。


スポンサーリンク


添付ファイル: filegenpwd.zip 583件 [詳細] file01.png 697件 [詳細]

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2025-03-09 (日) 14:41:58