LoginSignup
0
0

More than 3 years have passed since last update.

PowerShell CoreではSystem.Web.Security.Membershipは利用できない

Last updated at Posted at 2019-11-06

メモ。表題の通りなのだけど、PowerShell CoreではSystem.Web.Security.Membershipは利用できないことを確認した。

現象

PowerShellでパスワード文字列生成方法として、Web上ではしばしば次のようなものが紹介されている。

PS> Add-type -AssemblyName System.Web
PS> [System.Web.Security.Membership]::GeneratePassword(10,3)

PowerShell 5.1.18362.145環境では上記でうまくいったが、PowerShell Core 6.2.3環境でこれを実行したところ、次のようなエラーになった。

Unable to find type [System.Web.Security.Membership].
At line:1 char:13

原因

これはそもそも.NET Coreの非互換によるらしい。

I would not expect it to work with PowerShell core as it was uses .net framework features that would not be included in core.
Not compatible with PowerShell Core · Issue #23 · davotronic5000/PowerShell_Credential_Manager

代替策

Redditの「Generate good password : PowerShell」で、以下のようにアッセンブリに頼らずPowerShellでランダム文字列を生成する代替策が提案されている。

$length = 15

$letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ".ToCharArray()
$uppers = "ABCDEFGHJKLMNPQRSTUVWXYZ".ToCharArray()
$lowers = "abcdefghijkmnopqrstuvwxyz".ToCharArray()
$digits = "23456789".ToCharArray()
$symbols = "_-+=@$%".ToCharArray()

$chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789_-+=@$%".ToCharArray()

do {
    $pwdChars = "".ToCharArray()
    $goodPassword = $false
    $hasDigit = $false
    $hasSymbol = $false
    $pwdChars += (Get-Random -InputObject $uppers -Count 1)
    for ($i=1; $i -lt $length; $i++) {
        $char = Get-Random -InputObject $chars -Count 1
        if ($digits -contains $char) { $hasDigit = $true }
        if ($symbols -contains $char) { $hasSymbol = $true }
        $pwdChars += $char
    }
    $pwdChars += (Get-Random -InputObject $lowers -Count 1)
    $password = $pwdChars -join ""
    $goodPassword = $hasDigit -and $hasSymbol
} until ($goodPassword)

Set-Clipboard $password

PowerShellではなくなるけど、「asp.net - Alternative to System.Web.Security.Membership.GeneratePassword in aspnetcore (netcoreapp1.0) - Stack Overflow」ではSystem.Web.Security.Membership.GeneratePasswordのコードを元にした代替コードが示されている。

参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0