Azure-CLI/Powershell Password requirments

时光怂恿深爱的人放手 提交于 2019-12-01 14:22:58

I'm not sure if this can be done with just one regex.. As an alternative, below a small helper function to do the tests.

function Test-AdminPassword {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory=$true)]
        [string]$Password,

        [Parameter(Position = 1)]
        [int]$Requirements = 5
    )
    $result = 0

    # test length between 12 and 24
    if ($Password.Length -in 12..24) {
        $result++
    }
    # test uppercase
    if (($Password -creplace '[^A-Z]', '').Length -ge 3) {
        $result++
    }
    # test lowercase
    if (($Password -creplace '[^a-z]', '').Length -ge 3) {
        $result++
    }
    # test digits
    if (($Password -replace '[^0-9]', '').Length -ge 3) {
        $result++
    }
    # test special characters
    if (($Password -creplace '[^!@$#%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ]', '').Length -ge 3) {
        $result++
    }

    # return $true if the password complies with at least $requirements
    return ($result -ge $Requirements)
}

do {
    $AdminPassword = Read-Host -Prompt "Please insert an Admin Password (must have the 3 lower case characters, 3 upper case characters, 3 digits and 3 special characters)"
} until (Test-AdminPassword $AdminPassword)

For the special characters,the regular expression should be the one in the code below.

You need to use single quote for your regular expression:

do
{
$s=Read-Host -Prompt "please enter a password"
}
until($s -like '[A-Z][A-Z][A-Z][a-z][a-z][a-z][0-9][0-9][0-9][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ]')

write-host "complete entering password"

Test result as below:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!