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
For the special characters,the regular expression should be the one in the code below.
You need to use single quote for your regular expression:
do
{
$s=Read-Host -Prompt "please enter a password"
}
until($s -like '[A-Z][A-Z][A-Z][a-z][a-z][a-z][0-9][0-9][0-9][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ][!@$#$%^&*()_+\-\=`[`]{};'':`"\\|,.<>\/? ]')
write-host "complete entering password"
Test result as below:
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)