用于检查密码标准的 Powershell 脚本
Powershell script for checking password criteria
我正在编写一个创建本地帐户的脚本,为用户提供两次输入密码的机会,然后将根据类似于网络帐户的标准仔细检查密码。
我整理了下面的脚本,几乎 可以做到这一点。它似乎能够满足我要插入的一些标准。
我正在寻找的是比较第一个条目和确认条目,并根据以下组合检查他们输入的内容,如果不符合标准则拒绝它们,如果符合则接受它们:
至少 1 个大写字母,至少 1 个特殊字符,至少 1 个数字,至少 8 个字符 - 例如“Monkeypuzzle1!”
至少 1 个特殊字符,至少 1 个数字,至少 8 个字符 - 例如“monkeypuzzle1!”
至少 1 个大写字母,1 个小写字母,至少 1 个特殊字符,至少 8 个字符 - 例如“猴子拼图!”
至少 1 个大写字母,1 个小写字母,至少 1 个数字,至少 8 个字符 - "Monkeypuzzle1"
到目前为止我已经成功地比较了第一个条目和确认条目并且似乎能够仔细检查 1 x cap, 1 x lowercase, 1 x special, 1 x number, 8 characters 条件和 1 x 大写、1x 小写字母、1 x 数字、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的答案的扩展,似乎完成了工作,似乎已经通过了我目前能想到的所有测试,不得不修改我寻找特殊的方法通过查找不是大写、小写或数字的字符来查找字符。但是那个伯爵的主意真是太棒了!
$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.'
我正在编写一个创建本地帐户的脚本,为用户提供两次输入密码的机会,然后将根据类似于网络帐户的标准仔细检查密码。 我整理了下面的脚本,几乎 可以做到这一点。它似乎能够满足我要插入的一些标准。
我正在寻找的是比较第一个条目和确认条目,并根据以下组合检查他们输入的内容,如果不符合标准则拒绝它们,如果符合则接受它们:
至少 1 个大写字母,至少 1 个特殊字符,至少 1 个数字,至少 8 个字符 - 例如“Monkeypuzzle1!”
至少 1 个特殊字符,至少 1 个数字,至少 8 个字符 - 例如“monkeypuzzle1!”
至少 1 个大写字母,1 个小写字母,至少 1 个特殊字符,至少 8 个字符 - 例如“猴子拼图!”
至少 1 个大写字母,1 个小写字母,至少 1 个数字,至少 8 个字符 - "Monkeypuzzle1"
到目前为止我已经成功地比较了第一个条目和确认条目并且似乎能够仔细检查 1 x cap, 1 x lowercase, 1 x special, 1 x number, 8 characters 条件和 1 x 大写、1x 小写字母、1 x 数字、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的答案的扩展,似乎完成了工作,似乎已经通过了我目前能想到的所有测试,不得不修改我寻找特殊的方法通过查找不是大写、小写或数字的字符来查找字符。但是那个伯爵的主意真是太棒了!
$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.'