Regex Azure CLI\Powershell如何排除要求

Regex Azure CLI\Powershell如何排除要求,regex,azure,powershell,azure-cli,azure-cli2,Regex,Azure,Powershell,Azure Cli,Azure Cli2,作为我最后一个问题的继续。 我得到了这段代码,但现在我想知道如何排除一些需求 function Test-AdminPassword { [CmdletBinding()] Param( [Parameter(Position = 0, Mandatory=$true)] [string]$Password, [Parameter(Position = 1)] [int]$Requirements = 5 ) $result = 0 # test length

作为我最后一个问题的继续。 我得到了这段代码,但现在我想知道如何排除一些需求

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)
}
功能测试密码{
[CmdletBinding()]
Param(
[参数(位置=0,强制=true)]
[字符串]$Password,
[参数(位置=1)]
[int]$Requirements=5
)
$result=0
#测试长度介于12和24之间
如果($Password.Length-in 12..24){
$result++
}
#测试大写字母
如果($Password-creplace'[^A-Z]','').Length-ge 3){
$result++
}
#测试小写字母
如果($Password-creplace'[^a-z]','').Length-ge 3){
$result++
}
#测试数字
如果($Password-替换“[^0-9]”,“”).Length-ge 3){
$result++
}
#测试特殊字符
如果($Password-creplace'[^!@$\\%^&*()\+\-=\[\]{};“”:“\\\\\”,.\/?”,“”)。长度-ge 3){
$result++
}
#如果密码至少符合$要求,则返回$true
返回($result-ge$要求)
}

我现在的问题是如何编辑它,以便排除特殊字符,因为在管理员用户名中只能使用小写字符。 这一个使用了更多的参数供您使用,但是它可以用于测试管理员用户名和输入的密码

function Test-AdminInput {
    [CmdletBinding(DefaultParameterSetName = "ByCase")]
    Param(
        [Parameter(Position = 0, Mandatory=$true)]
        [string]$NameOrPassword,

        # Instead of these default numbers, you can set them all to 0
        # if you like. That way, omitting them from the call will skip the test.
        [int]$MinLength = 12,
        [int]$MaxLength = 24,
        [int]$MinDigits = 3,
        [int]$MinSpecial = 3,

        [Parameter(ParameterSetName = "ByCase")]
        [int]$MinUpperCase = 3,
        [Parameter(ParameterSetName = "ByCase")]
        [int]$MinLowerCase = 3,
        [Parameter(ParameterSetName = "ByCaseRestrict")]
        [ValidateSet ("AllUpperCase","AllLowerCase","Any")]
        [string]$RestrictCase = "Any"
    )
    # test $MinLength
    if ($MinLength -gt 0 -and $NameOrPassword.Length -lt $MinLength) {
        Write-Warning "You need at least $MinLength characters"
        return $false
    }

    # test $MaxLength
    if ($MaxLength -gt 0 -and $NameOrPassword.Length -gt $MaxLength) {
        Write-Warning "You cannot use more than $MaxLength characters"
        return $false
    }

    # test Restricted casing
    if ($PSCmdlet.ParameterSetName -eq "ByCaseRestrict") {
        switch ($RestrictCase) {
            "AllUpperCase" { if ($NameOrPassword.ToUpperInvariant() -cne $NameOrPassword)  { 
                             Write-Warning "You must use upper-case characters only"; return $false } }
            "AllLowerCase" { if ($NameOrPassword.ToLowerInvariant() -cne $NameOrPassword)  { 
                             Write-Warning "You must use lower-case characters only"; return $false } }
        }
    }
    else {
        # test minimum uppercase
        if ($MinUpperCase -gt 0) {
            if (($NameOrPassword -creplace '[^A-Z]', '').Length -lt $MinUpperCase) { 
                Write-Warning "You must use at least $MinUpperCase upper-case characters" 
                return $false
            }
        }
        # test minimum lowercase
        if ($MinLowerCase -gt 0) {
            if (($NameOrPassword -creplace '[^a-z]', '').Length -lt $MinLowerCase) { 
                Write-Warning "You must use at least $MinLowerCase lower-case characters" 
                return $false
            }
        }
    }

    # test digits
    if ($MinDigits -gt 0) {
        if (($NameOrPassword -replace '[^0-9]', '').Length -lt $MinDigits) {
            Write-Warning "You must use at least $MinDigits digits (0-9)" 
            return $false
        }
    }
    # test special characters
    if ($MinSpecial -gt 0) {
        if (($NameOrPassword -creplace '[^!@$#%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ]', '').Length -lt $MinSpecial) {
            Write-Warning "You must use at least $MinSpecial special characters (!@$#%^&*()_+-=[]{};'`":\|,.<>/? )" 
            return $false
        }
    }

    # If you get here, all tests succeeded
    return $true
}
要测试用户名,请按如下方式使用:

do {
    $input = 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)"
    $result = Test-AdminInput $input -MinLength 12 -MaxLength 24 -MinUpperCase 3 -MinLowerCase 3 -MinDigits 3 -MinSpecial 3
} until ($result)
do {
    $input = Read-Host -Prompt "Please insert an Admin User name (must have only lower case characters, 3 digits)"
    $result = Test-AdminInput $input -MinLength 12 -MaxLength 24 -MinDigits 3 -RestrictCase AllLowerCase
} until ($result)

这是我之前函数的完整重写。 这一个使用了更多的参数供您使用,但是它可以用于测试管理员用户名和输入的密码

function Test-AdminInput {
    [CmdletBinding(DefaultParameterSetName = "ByCase")]
    Param(
        [Parameter(Position = 0, Mandatory=$true)]
        [string]$NameOrPassword,

        # Instead of these default numbers, you can set them all to 0
        # if you like. That way, omitting them from the call will skip the test.
        [int]$MinLength = 12,
        [int]$MaxLength = 24,
        [int]$MinDigits = 3,
        [int]$MinSpecial = 3,

        [Parameter(ParameterSetName = "ByCase")]
        [int]$MinUpperCase = 3,
        [Parameter(ParameterSetName = "ByCase")]
        [int]$MinLowerCase = 3,
        [Parameter(ParameterSetName = "ByCaseRestrict")]
        [ValidateSet ("AllUpperCase","AllLowerCase","Any")]
        [string]$RestrictCase = "Any"
    )
    # test $MinLength
    if ($MinLength -gt 0 -and $NameOrPassword.Length -lt $MinLength) {
        Write-Warning "You need at least $MinLength characters"
        return $false
    }

    # test $MaxLength
    if ($MaxLength -gt 0 -and $NameOrPassword.Length -gt $MaxLength) {
        Write-Warning "You cannot use more than $MaxLength characters"
        return $false
    }

    # test Restricted casing
    if ($PSCmdlet.ParameterSetName -eq "ByCaseRestrict") {
        switch ($RestrictCase) {
            "AllUpperCase" { if ($NameOrPassword.ToUpperInvariant() -cne $NameOrPassword)  { 
                             Write-Warning "You must use upper-case characters only"; return $false } }
            "AllLowerCase" { if ($NameOrPassword.ToLowerInvariant() -cne $NameOrPassword)  { 
                             Write-Warning "You must use lower-case characters only"; return $false } }
        }
    }
    else {
        # test minimum uppercase
        if ($MinUpperCase -gt 0) {
            if (($NameOrPassword -creplace '[^A-Z]', '').Length -lt $MinUpperCase) { 
                Write-Warning "You must use at least $MinUpperCase upper-case characters" 
                return $false
            }
        }
        # test minimum lowercase
        if ($MinLowerCase -gt 0) {
            if (($NameOrPassword -creplace '[^a-z]', '').Length -lt $MinLowerCase) { 
                Write-Warning "You must use at least $MinLowerCase lower-case characters" 
                return $false
            }
        }
    }

    # test digits
    if ($MinDigits -gt 0) {
        if (($NameOrPassword -replace '[^0-9]', '').Length -lt $MinDigits) {
            Write-Warning "You must use at least $MinDigits digits (0-9)" 
            return $false
        }
    }
    # test special characters
    if ($MinSpecial -gt 0) {
        if (($NameOrPassword -creplace '[^!@$#%^&*()_+\-=\[\]{};'':"\\|,.<>\/? ]', '').Length -lt $MinSpecial) {
            Write-Warning "You must use at least $MinSpecial special characters (!@$#%^&*()_+-=[]{};'`":\|,.<>/? )" 
            return $false
        }
    }

    # If you get here, all tests succeeded
    return $true
}
要测试用户名,请按如下方式使用:

do {
    $input = 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)"
    $result = Test-AdminInput $input -MinLength 12 -MaxLength 24 -MinUpperCase 3 -MinLowerCase 3 -MinDigits 3 -MinSpecial 3
} until ($result)
do {
    $input = Read-Host -Prompt "Please insert an Admin User name (must have only lower case characters, 3 digits)"
    $result = Test-AdminInput $input -MinLength 12 -MaxLength 24 -MinDigits 3 -RestrictCase AllLowerCase
} until ($result)

感谢anwser!我尝试扩展该函数,以便它也可以用于存储帐户我唯一拥有的是它只能有小写字符和数字,因此当您输入任何其他内容时,它将继续运行,那么您是否知道如何修复它,或者我是否应该制作一个新的qeustion?@Brynn为此,您想要一个新的qeustion更简单的解决方案。如果您唯一的限制是
小写和/或数字
,您可以这样做:
执行{$input=Read Host-Prompt“请插入用户名(必须只有小写字符和/或数字)”}直到($input-cmatch'\A(?[A-z0-9]+)\Z')
Hi@Theo可能还有一种方法可以在3到24个字符之间添加长度吗?我已经尝试添加
.length-ge 3
当然。只需在括号之间的匹配测试之后添加
-和$input.length-在3..24
中。感谢anwser!我尝试扩展该函数,以便它也可以用于存储帐户我所拥有的是,它只能有小写字符和数字,因此,当你输入任何其他内容时,它将继续,然后你是否知道如何修复它,或者我是否应该进行新的验证?@Brynn为此,你需要一个更简单的解决方案。如果你唯一的限制是
小写和/或数字,那么你可以做一些事情如下所示:
do{$input=Read Host-Prompt“请插入用户名(必须只有小写字符和/或数字)”}直到($input-cmatch'\A(?[A-z0-9]+)\Z')
Hi@Theo也许还有一种方法可以在3到24个字符之间添加长度?我已经尝试添加
.length-ge 3
当然。只需在括号之间的匹配测试后在3..24
中添加
-和$input.length-。