windows: fix first-boot races and non-idempotent stages

A from-scratch rebuild surfaced three defects that only appear on a clean
deployment:

- dc01-post: `Set-ADObject -Identity <sAMAccountName>` raises
  ADIdentityNotFoundException even right after New-ADUser, so resolve the
  DN once and reuse it. The stage aborted before DNS, flags, DHCP and the
  audit policy, leaving a half-built domain.
- bootstrap ran the role script at first boot while `provision` also ran
  it. fs01 tried to join a domain that did not exist yet, and dc01 ran two
  promotions concurrently; the loser wrote "FAIL Stream was not readable".
  Roles are now only started by the harness, which orders them.
- fs01: the `.url` MIME existence test read the staticContent element
  (-Name '.') instead of its collection, so any re-run died on a duplicate
  entry. Test -Name 'collection'.

Every stage now takes a PID-based lock: a duplicate start exits instead of
racing the real one.

Validated on a fresh build: doctor 0 warnings, and the full path
(site -> SMB -> AS-REP -> Kerberoast -> WinRM -> ACL -> replication ->
DCSync) reproduces.
This commit is contained in:
2026-10-24 16:38:37 -04:00
parent 95d70fa5a4
commit ca72e1aa2e
7 changed files with 69 additions and 5 deletions
+11 -2
View File
@@ -4,6 +4,10 @@
. "$PSScriptRoot\..\lib\common.ps1"
$self = $PSCommandPath
if (-not (Enter-StageLock $self)) {
Write-LabLog 'stage already running on this guest; duplicate start ignored'
return
}
Write-LabLog 'dc01: post stage starting'
try {
@@ -119,14 +123,17 @@ try {
$domainAcl.CommitChanges()
Write-LabLog 'dc01: Sauvegarde-AD granted replication rights'
# Set-ADObject does not resolve a sAMAccountName reliably (it raises
# ADIdentityNotFoundException), so resolve the DN once and use it below.
$plainDn = (Get-ADUser 'm.legault').DistinguishedName
# m.legault must not carry a stale adminCount flag from an earlier design,
# or SDProp would keep treating the account as protected and wipe the ACE.
Set-ADObject -Identity 'm.legault' -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue
Set-ADObject -Identity $plainDn -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue
# Realistic delegation: the service account manages a plain user who is a
# member of the server-admin group. m.legault is NOT in a protected group,
# so SDProp leaves this ACE alone — unlike the Domain Admin target above.
$plainDn = (Get-ADUser 'm.legault').DistinguishedName
if (-not (dsacls.exe $plainDn | Select-String 'svc_backup')) {
$null = dsacls.exe $plainDn /G '@@NETBIOS@@\svc_backup:GA'
Write-LabLog 'dc01: GenericAll granted to svc_backup over m.legault'
@@ -240,4 +247,6 @@ try {
Complete-Stage $self 'ok'
} catch {
Fail-Stage $self $_.Exception.Message
} finally {
Exit-StageLock $self
}