107 lines
4.1 KiB
PowerShell
107 lines
4.1 KiB
PowerShell
[CmdletBinding(SupportsShouldProcess)]
|
|
param(
|
|
[string]$DomainController = $env:COMPUTERNAME,
|
|
[string]$BaseDn = 'DC=lci,DC=lasalle,DC=mx',
|
|
[string]$PolicyName = 'SGU - Institutional password authority',
|
|
[int]$Precedence = 100
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
Import-Module ActiveDirectory -ErrorAction Stop
|
|
|
|
$domain = Get-ADDomain -Server $DomainController -ErrorAction Stop
|
|
if ($domain.DistinguishedName -ne $BaseDn) {
|
|
throw "BaseDn does not match the domain served by $DomainController."
|
|
}
|
|
if ($domain.DomainMode -lt 'Windows2012Domain') {
|
|
throw 'Fine-grained password policies require Windows Server 2012 domain functional level or newer.'
|
|
}
|
|
|
|
# AD's default password policy must remain intact for non-SGU accounts.
|
|
# The broker validates the exact password with SGO before resetting the AD
|
|
# verifier. These global security groups are assigned before SetPassword.
|
|
$groups = @(
|
|
"CN=SGU-Alumnos,OU=Alumnos,OU=Usuarios-SGU,$BaseDn",
|
|
"CN=SGU-Administrativos,OU=Administrativos,OU=Usuarios-SGU,$BaseDn",
|
|
"CN=SGU-Docentes,OU=Docentes,OU=Usuarios-SGU,$BaseDn"
|
|
)
|
|
foreach ($groupDn in $groups) {
|
|
$group = Get-ADGroup -Identity $groupDn -Server $DomainController -ErrorAction Stop
|
|
if ($group.GroupCategory -ne 'Security' -or $group.GroupScope -ne 'Global') {
|
|
throw "The password-policy subject must be a global security group: $groupDn"
|
|
}
|
|
}
|
|
|
|
$desired = @{
|
|
Precedence = $Precedence
|
|
ComplexityEnabled = $false
|
|
ReversibleEncryptionEnabled = $false
|
|
MinPasswordLength = 1
|
|
PasswordHistoryCount = 0
|
|
MinPasswordAge = [TimeSpan]::Zero
|
|
MaxPasswordAge = [TimeSpan]::Zero
|
|
LockoutThreshold = 0
|
|
LockoutDuration = [TimeSpan]::FromMinutes(10)
|
|
LockoutObservationWindow = [TimeSpan]::FromMinutes(10)
|
|
}
|
|
|
|
$policy = Get-ADFineGrainedPasswordPolicy -Filter * -Server $DomainController |
|
|
Where-Object { $_.Name -eq $PolicyName } |
|
|
Select-Object -First 1
|
|
if (-not $policy) {
|
|
if ($PSCmdlet.ShouldProcess($PolicyName, 'Create password policy limited to SGU role groups')) {
|
|
New-ADFineGrainedPasswordPolicy -Name $PolicyName @desired -Server $DomainController `
|
|
-Description 'SGO validates the exact institutional password; AD must not reject its complexity, length, history, or age.' `
|
|
-ProtectedFromAccidentalDeletion $true | Out-Null
|
|
}
|
|
}
|
|
else {
|
|
$different = @($desired.Keys | Where-Object { $policy.$_ -ne $desired[$_] })
|
|
if ($different.Count -gt 0 -and
|
|
$PSCmdlet.ShouldProcess($PolicyName, "Restore managed settings: $($different -join ', ')")) {
|
|
Set-ADFineGrainedPasswordPolicy -Identity $policy @desired -Server $DomainController | Out-Null
|
|
}
|
|
}
|
|
|
|
$policy = Get-ADFineGrainedPasswordPolicy -Filter * -Properties AppliesTo `
|
|
-Server $DomainController |
|
|
Where-Object { $_.Name -eq $PolicyName } |
|
|
Select-Object -First 1
|
|
if (-not $policy) {
|
|
if ($WhatIfPreference) { return }
|
|
throw "The fine-grained policy $PolicyName was not created."
|
|
}
|
|
foreach ($groupDn in $groups) {
|
|
if (@($policy.AppliesTo) -notcontains $groupDn) {
|
|
if ($PSCmdlet.ShouldProcess($groupDn, "Apply $PolicyName")) {
|
|
Add-ADFineGrainedPasswordPolicySubject -Identity $policy -Subjects $groupDn `
|
|
-Server $DomainController -ErrorAction Stop
|
|
}
|
|
}
|
|
}
|
|
|
|
$policy = Get-ADFineGrainedPasswordPolicy -Identity $PolicyName -Properties AppliesTo `
|
|
-Server $DomainController -ErrorAction Stop
|
|
if (-not $WhatIfPreference) {
|
|
foreach ($key in $desired.Keys) {
|
|
if ($policy.$key -ne $desired[$key]) {
|
|
throw "Password policy setting $key did not match the expected value."
|
|
}
|
|
}
|
|
foreach ($groupDn in $groups) {
|
|
if (@($policy.AppliesTo) -notcontains $groupDn) {
|
|
throw "Password policy is not applied to $groupDn."
|
|
}
|
|
}
|
|
}
|
|
|
|
[pscustomobject]@{
|
|
Name = $policy.Name
|
|
Precedence = $policy.Precedence
|
|
ComplexityEnabled = $policy.ComplexityEnabled
|
|
MinPasswordLength = $policy.MinPasswordLength
|
|
PasswordHistoryCount = $policy.PasswordHistoryCount
|
|
MaxPasswordAge = $policy.MaxPasswordAge
|
|
Subjects = @($policy.AppliesTo)
|
|
}
|