@Andreas Baumgarten Sorry for the late reply.
Yes, I am creating a script for IT Support create new AD user.
I created a Function TestPasswordComplexy.
This script works almost perfectly. Function TestPasswordComplexy gives error on screen:
New-ADUser: The password does not meet the length, complexity, or history requirement of the domain.
At C: \ Users \ Administrator \ Documents \ scripts \ tests \ Versions \ Create_user_user_v.1.4 - test.ps1: 123 char: 1
- New-ADUser -SamAccountName $ SamAccountName -Name $ DisplayName -Displa ...
The script still prints the outputs below before displaying the above error:
Passwords match.
.........
Valid password.
But it does not create the user.
Function TestPasswordComplexy {
do {
$secpass = Read-Host "Type password" -AsSecureString
$secpass2 = Read-Host "Retype password" -AsSecureString
$secpass_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($secpass))
$secpass2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($secpass2))
}
while ($secpass_text -cne $secpass2_text)
Write-Host "Passwords match." -F green
Write-Host "........." -F green
if ($null -eq (Get-Module -listavailable activedirectory))
{
# handle problem, next step required AD module.
}
$policy = Get-ADDefaultDomainPasswordPolicy
$complexityRulesMet = 0
if ($policy.ComplexityEnabled)
{
# assume 3 in 4 complexity
if ($secpass -cmatch '[a-z]') { $complexityRulesMet += 1 }
if ($secpass -cmatch '[A-Z]') { $complexityRulesMet += 1 }
if ($secpass -match '\d') { $complexityRulesMet += 1 }
if ($secpass -match '[`~!@#$%^&*()_+-=\\{}|;'''':",./<>?\[\]]') { $complexityRulesMet += 1 }
}
else
{
# domain doesn't enforce complexity requirement
$complexityRulesMet = 4
}
if($secpass.Length -ge $policy.MinPasswordLength -and $complexityRulesMet -ge 3)
{
Write-Host "Valid password." -ForegroundColor Green
Write-Host "...." -F green
Write-Host "........." -F green
}
else
{
Write-Host "Password not valid! Password must meet complexity requirements." -ForegroundColor Red
Write-Host ""
Write-Host ""
TestPasswordComplexy
}
}
TestPasswordComplexy
If I remove / comment on Function, it works. However, if password failed, the script returns from the beginning of the Menu. I would like you to return in the step of entering the password after the failure.
If you want the complete code, tell me, I will insert it here.
Please give me a helping hand or a way forward.
Thanks!