Azure-CLI/Powershell Password requirments

后端 未结 2 1257
忘掉有多难
忘掉有多难 2021-01-17 01:16

So i am trying to make a variable match the password requirements for making a virtual machine in azure CLI

to do this it must have 3 of the following !uppercase, 1

2条回答
  •  一生所求
    2021-01-17 01:59

    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)
    

提交回复
热议问题