检查密码标准的Powershell脚本



我正在编写一个创建本地帐户的脚本,为用户提供两次输入密码的机会,然后将根据类似于网络帐户的标准进行审查。我把下面的脚本放在一起,它几乎可以做到这一点。它似乎能够满足我想要加入的一些标准。

我要找的是比较第一个和确认条目,并检查他们根据以下组合输入的内容,如果不符合标准则拒绝它们,如果符合标准则接受它们:

至少1个大写字母,至少1个特殊字符,至少1个数字,至少8个字符-例如"Monkeypuzzle1!">

至少1个特殊字符,至少1个数字,至少8个字符-例如"monkeypuzzle1!">

至少1个大写字母,1个小写字母,至少1个特殊字符,至少8个字符-例如"Monkeypuzzle!">

至少1个大写字母,1个小写字母,至少1个数字,至少8个字符- ';Monkeypuzzle1">

到目前为止,我已经成功地比较了第一个条目和确认条目,并且似乎能够仔细检查1 x大写,1 x小写,1 x特殊,1 x数字,8个字符标准和1个大写字母,1个小写字母,1个数字,8个字符标准,但不是其他两个。我试了好几种方法,但到目前为止都破解不了。
while($true)
{
        try
        {
            # Loop until a valid password is entered.
            $Entered_Password_01 = Read-Host "Enter a password: "-AsSecureString
            $Entered_Password_01_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Entered_Password_01))
            Write-Host ""
            $Pass_criteria_01 = (-not(($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text.length -ge 7) -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match 'd')))
            $Pass_criteria_02 = (-not(($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text.length -ge 7) -and ($Entered_Password_01_text -match 'd') -and ($Entered_Password_01_text -match '!|@|#|%|^|&|$')))
            $Pass_criteria_03 = (-not(($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text.length -ge 7) -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match '!|@|#|%|^|&|$')))
            $Pass_criteria_04 = (-not(($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text.length -ge 7) -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match 'd') -and ($Entered_Password_01_text -match '!|@|#|%|^|&|$')))
            if (        $Pass_criteria_01 -or
                        $Pass_criteria_02 -or
                        $Pass_criteria_03 -or
                        $Pass_criteria_04)
            {throw}
            $Entered_Password_02 = Read-Host "Confirm password: "-AsSecureString
            $Entered_Password_02_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Entered_Password_02))
            Write-Host ""
            if ($Entered_Password_01_text -ceq $Entered_Password_02_text) {Write-host "successful test"}
            Else
            {throw}
            break
        }
        catch
        {
            Write-Host ""
            Write-Host "Invalid Password." -ForegroundColor Red
        }
}

我基本上是在告诉它按照4组标准依次检查它,如果不符合就把它拦截下来,如果符合就让它通过。

如果有人对如何实现这一目标有任何建议,请告诉我。

欢呼。

mklement0's的扩展答案,似乎做的工作,似乎已经通过了所有的测试,我能想到的时刻,不得不修改我的方法来寻找特殊字符,寻找字符不是大写,小写或数字。但是那个计数的主意太棒了!

$Entered_Password_01_text = 'Monkeypuzzle1!' # Experiment with different values here.
$Special_Chars = "[^a-zA-Z0-9]" #check for special characters, by looking for not regular characters or numbers.
$Password_Criteria = (
  (
                (($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match 'd')),                                                         # At least 1 x lowercase, at least 1 x uppercase, at least 1 x number and 8 characters.
                (($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text -match 'd') -and ($Entered_Password_01_text -cmatch $Special_Chars)),                                                  # At least 1 x lowercase, at least 1 x number, at least 1 x special characters and 8 characters.
                (($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -cmatch $Special_Chars)),                                              # At least 1 x lowercase, at least 1 x uppercase, at least 1 x special characters and 8 characters.
                (($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match 'd') -and ($Entered_Password_01_text -cmatch $Special_Chars)),                                                  # At least 1 x uppercase, at least 1 x number, at least 1 x special characters and 8 characters.
                (($Entered_Password_01_text -cmatch '[a-z]') -and ($Entered_Password_01_text -cmatch '[A-Z]') -and ($Entered_Password_01_text -match 'd') -and ($Entered_Password_01_text -cmatch $Special_Chars))  # At least 1 x lowercase, at least 1 x uppercase, at least 1 x number, at least 1 x special characters and 8 characters.
  ) -eq $true
).Count
$Password_Complexity_Validated = $Entered_Password_01_text.Length -ge 8 -and $Password_Criteria -ge 1
if (-not $Password_Complexity_Validated) { Write-host  'Invalid password.' -ForegroundColor Red
}Else{
Write-host 'Password is valid.'-ForegroundColor Green } 

只关注核心逻辑,尝试以下操作:

$Entered_Password_01_text = 'foooooo1!' # Experiment with different values here.
$numCriteriaMet = (
  (
    ($Entered_Password_01_text -cmatch '[A-Z]'),     # at least 1 uppercase letter
    ($Entered_Password_01_text -match '[!@#%^&$]'),  # at least 1 special char.
    ($Entered_Password_01_text -match '[0-9]')       # at least 1 digit
  ) -eq $true
).Count
# A password is valid if it is at least 8 chars. long
# and meets at least 2 of the 3 criteria above.
$validOverall = $Entered_Password_01_text.Length -ge 8 -and $numCriteriaMet -ge 2
if (-not $validOverall) { throw 'Invalid password.' }
Write-Verbose -Verbose 'Password is valid.'

最新更新