所以我试图让一个变量与在 azure CLI 中创建虚拟机的密码要求相匹配
为此,它必须具有以下 3 个!大写字母、1 个小写字母、1 个特殊字符或数字
这是我试图更改的主要代码
$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)"
这是我更改的代码,但无法正常工作
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
通过写入匹配线我知道它何时成功
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
预先感谢您阅读本文并发布您的答案!
我不确定这是否可以只用一个正则表达式来完成..
作为替代方案,下面是一个小辅助函数来进行测试。
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)