为避免由于密码不符合域密码复杂性规则而创建禁用用户,您可以使用此辅助函数:
function Test-DomainPassword {
# see: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/password-must-meet-complexity-requirements
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Password,
[string]$SamAccountName = $null,
[string]$DisplayName = $null
)
$PasswordPolicy = Get-ADDefaultDomainPasswordPolicy -ErrorAction SilentlyContinue
if ($Password.Length -lt $PasswordPolicy.MinPasswordLength) {
Write-Verbose "Password '$Password' is too short. Minimal length is $($PasswordPolicy.MinPasswordLength)"
return $false
}
if (($SamAccountName) -and ($Password -match [regex]::Escape($SamAccountName))) {
Write-Verbose "The password '$Password' includes the users SamAccountName"
return $false
}
if ($DisplayName) {
# The displayName is parsed for delimiters: commas, periods, dashes or hyphens, underscores, spaces, pound signs, and tabs.
# If any of these delimiters are found, the displayName is split and all parsed sections (tokens) are confirmed not to be
# included in the password.
# Tokens that are shorter than three characters are ignored, and substrings of the tokens aren't checked.
$tokens = $DisplayName.Split(",.-,_ #`t")
foreach ($token in $tokens) {
if (($token) -and ($token.Length -ge 3) -and ($Password -match [regex]::Escape($token))) {
Write-Verbose "The password '$Password' includes (part of) the users DisplayName"
return $false
}
}
}
if ($PasswordPolicy.ComplexityEnabled) {
# see: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/bb726984(v=technet.10)?redirectedfrom=MSDN
# chapter 'Passwords Must Meet Complexity Requirements':
# Passwords must use three of the four available character types:
# lowercase letters, uppercase letters, numbers, and symbols.
$failures = @()
# check for presence of
# - Uppercase: A through Z, with diacritic marks, Greek and Cyrillic characters
if ($Password -cnotmatch "[A-Z\p{Lu}\s]") {
$failures += "- The password is missing Uppercase characters"
}
# - Lowercase: a through z, sharp-s, with diacritic marks, Greek and Cyrillic characters
if ($Password -cnotmatch "[a-z\p{Ll}\s]") {
$failures += "- The password is missing Lowercase characters"
}
# - Base 10 digits (0 through 9)
if ($Password -notmatch "[\d]") {
$failures += "- The password is missing digits (0-9)"
}
# - Nonalphanumeric characters: ~!@#$%^&*_-+=`|\(){}[]:;"'<>,.?/
if ($Password -notmatch "[^\w]") {
$failures += "- The password is missing Nonalphanumeric characters: ~!@#$%^&*_-+=`|\(){}[]:;`"'<>,.?/"
}
# test if we have more than 1 mismatch (password needs at least 3 out of 4 to be OK)
if ($failures.Count -gt 1) {
Write-Verbose "The password '$Password' failed because:`r`n{0}" -f ($failures -join "`r`n")
return $false
}
}
$true
}
像这样使用它:
# both parameters -SamAccountName and -DisplayName are optional
if (Test-DomainPassword -Password 'abc' -SamAccountName 'test1' -DisplayName 'Paul Test' -Verbose) {
# the password is OK, create the new user here:
$userParams = @{
SamAccountName = 'test1'
GivenName = 'Paul'
Surname = 'Test'
DisplayName = 'Paul Test'
AccountPassword = ConvertTo-SecureString -String 'abc' -AsPlainText -force
Enabled = $true
# etcetera
}
New-ADUser @userParams
}
else {
Write-Warning "User 'test1' NOT created because the password did not pass the test"
}