Azure-CLI/Powershell密码要求 [英] Azure-CLI/Powershell Password requirments

查看:95
本文介绍了Azure-CLI/Powershell密码要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使一个变量与在Azure命令行中制作虚拟机的密码要求相符

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

要执行此操作,必须使用以下三个大写字母,1个小写字母,1个特殊字符或数字

to do this it must have 3 of the following !uppercase, 1 lowercase 1 special character or a number

这是我要更改的主要代码

This is the main code i am trying to change

$AdminPassword = Read-Host -Prompt "Please insert a Admin Password (Password must have 3 of the following: 1 lower case character, 1 upper case character, 1 number and 1 special character)"

这是我更改的代码,但无法正常工作

This is the code i changed but isn't working correctly

do 
    {
        $AdminPassword = Read-Host -Prompt "Please insert a Admin Password (Password must have the 3 of the following: 1 lower case character, 1 upper case character, 1 number and 1 special character)"
    }
    until($AdminPassword -Like "[A-Z][A-Z][A-Z][a-z][a-z][a-z][0-9][0-9][0-9][!@$#$%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ][!@$#$%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ][!@$#$%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ]")
    Write Match

使用写匹配行,我知道它何时成功

with the write match line i know when it succeeds

我这里真正的麻烦是我要全部4个要求都3遍,所以3个大写字母3个小写字母等等 而且我也想拥有特殊字符 但是#注释所有后续代码

the real trouble i have here is that i want all 4 of the requirements 3 times so 3 uppercase's 3 lowercase's etc and i also want to have the special characters but the # comments all the code that follows out

在此先感谢您阅读并发布您的答案!

thanks in advance for reading this and posting your anwser!!!

推荐答案

我不确定是否可以仅使用一个正则表达式来完成. 或者,可以在下面的一个小助手功能下进行测试.

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)

这篇关于Azure-CLI/Powershell密码要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆