Azure CLI \ Powershell如何排除要求 [英] Azure CLI\Powershell How to exclude requirements

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

问题描述

作为我最后一个问题的续篇这一个.我把这个代码当作anwser,但现在我很想知道如何排除一些要求

So as a continuation of my last question This one. i got this code as a anwser but now i wold like how i could exclude some requirements

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

我现在的问题是如何进行编辑,以便排除特殊字符,因为在管理员用户名中只能使用小写字符

the question i have now is how can i edit this so i can exclude special characters because in a Admin Username you can use only lowercase characters

推荐答案

这里是我先前功能的完整重写.这个参数使用了很多参数供您使用,但是它既可以用来测试管理员用户名,也可以用来测试输入的密码.

Here's a complete re-write of my earlier function. This one uses a lot more parameters for you to play with, but then it can be used for testing both an admin username and also to test an inputted password.

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
}

要测试密码,请按以下方式使用它:

For testing Passwords use it like this:

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)

要测试用户名,请按以下方式使用它:

For testing User names use it like this:

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)

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

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