用于检查密码标准的PowerShell脚本

发布于 2025-01-21 14:00:52 字数 5064 浏览 0 评论 0 原文

我正在研究一个创建本地帐户的脚本,为用户提供了两次输入密码的机会,然后将根据类似于网络帐户的标准进行审查。 我已经整理了下面的脚本,可以几乎可以做到这一点。这似乎有能力遵守我要插入的某些标准。

我正在寻找的是比较第一个和确认条目并检查它们与以下组合所输入的内容,如果他们不拒绝它们,请拒绝它们。 t满足标准并接受它们,如果他们这样做:

至少1 x大写字母,至少1 x特殊字符,至少1 x数字,至少8个字符 - 例如“ monkeypuzzle1!”

至少1 x特殊字符,至少1 x数字,至少8个字符 - 例如“ monkeypuzzle1!”

至少1 x大写字母,1 x小写字母,至少1个特殊字符,至少8个字符 - 例如“ monkeypuzzle!”

至少1 x大写字母,1 x小写字母,至少1 x数字,至少8个字符 - “ monkeypuzzle1”,

我到目前为止已经成功比较了第一个条目和确认条目,并且似乎能够仔细检查 1 x帽,1 x小写,1 x特殊,1 x数字,8个字符标准和 1 x帽,1x小写,1 x数字,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的答案上的扩展似乎完成了这项工作,似乎已经通过了我目前能想到的所有测试,不得不通过寻找竞技场的角色来修改我寻找特殊角色的方法。 T大写,小写或数字。但是这个算是黄金!

$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 } 

I'm working on a script that creates local accounts, offering users the chance to enter a password twice, which will then be scrutinised against criteria similar to network accounts.
I've put together the script below, which can almost do this. It seems to be capable of some of the criteria I'm trying to plug in.

What I'm looking for is to compare the first and confirmation entry and to check what they have entered against the following combinations, reject them if they don't meet the criteria and accept them if they do:

At least 1 x capital letter, at least 1 x special character, at least 1 x number, at least 8 characters - e.g. "Monkeypuzzle1!"

At least 1 x special character, at least 1 x number, at least 8 characters - e.g. "monkeypuzzle1!"

At least 1 x capital letter, 1 x lowercase letter, at least 1 x special character, at least 8 characters - e.g. "Monkeypuzzle!"

At least 1 x capital letter, 1 x lowercase letter, at least 1 x number, at least 8 characters - "Monkeypuzzle1"

What I have so far successfully compares the first entry and the confirm entry and seems to be capable of scrutinising the 1 x cap, 1 x lowercase, 1 x special, 1 x number, 8 characters criteria and the 1 x cap, 1x lowercase, 1 x number, 8 characters criteria, but not the other two. I've tried several methods and so far I just can't crack it.

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 '!|@|#|%|^|&|

I'm basically trying to tell it to sequentially check it against 4 sets of criteria, snag it if it doesn't measure up and let it through if it does.

If anyone has any suggestions on how to achieve this please let me know.

Cheers.

Expansion on mklement0's answer, seems to do the job, seems to have passed all the tests I can think of at the moment, had to modify my method of looking for special characters by looking for characters that aren't uppercase, lowercase or numbers. But that count idea was golden!

$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 } 
))) $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 '!|@|#|%|^|&|

I'm basically trying to tell it to sequentially check it against 4 sets of criteria, snag it if it doesn't measure up and let it through if it does.

If anyone has any suggestions on how to achieve this please let me know.

Cheers.

Expansion on mklement0's answer, seems to do the job, seems to have passed all the tests I can think of at the moment, had to modify my method of looking for special characters by looking for characters that aren't uppercase, lowercase or numbers. But that count idea was golden!


)))
            $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 '!|@|#|%|^|&|

I'm basically trying to tell it to sequentially check it against 4 sets of criteria, snag it if it doesn't measure up and let it through if it does.

If anyone has any suggestions on how to achieve this please let me know.

Cheers.

Expansion on mklement0's answer, seems to do the job, seems to have passed all the tests I can think of at the moment, had to modify my method of looking for special characters by looking for characters that aren't uppercase, lowercase or numbers. But that count idea was golden!


)))
            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
        }
}

I'm basically trying to tell it to sequentially check it against 4 sets of criteria, snag it if it doesn't measure up and let it through if it does.

If anyone has any suggestions on how to achieve this please let me know.

Cheers.

Expansion on mklement0's answer, seems to do the job, seems to have passed all the tests I can think of at the moment, had to modify my method of looking for special characters by looking for characters that aren't uppercase, lowercase or numbers. But that count idea was golden!


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-01-28 14:00:52

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

$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.'

Focusing just on the core logic, try the following:

$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.'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文