Files
pentest.intro/session1/content/windows/lib/common.ps1
T
quoije ca72e1aa2e 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.
2026-10-24 16:38:37 -04:00

68 lines
2.3 KiB
PowerShell

# Norbourg lab — shared helpers for role scripts.
# Every role script writes a "<script>.done" marker so the host can wait
# without parsing output, and so a failure is visible instead of silent.
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
function Get-LabBase { 'C:\norbourg' }
function Write-LabLog {
param([string]$Message)
$line = "{0} {1}" -f (Get-Date -Format 'HH:mm:ss'), $Message
Write-Host $line
Add-Content -Path (Join-Path (Get-LabBase) 'provision.log') -Value $line
}
function Complete-Stage {
param([string]$Path, [string]$Message = 'ok')
Set-Content -Path "$Path.done" -Value $Message -Encoding ascii
Write-LabLog "stage done: $Message"
}
function Fail-Stage {
param([string]$Path, [string]$Message)
Set-Content -Path "$Path.done" -Value "FAIL $Message" -Encoding ascii
Write-LabLog "stage failed: $Message"
throw $Message
}
# A stage can be started twice: the harness schedules it, and a retried
# transport call or a manual run can schedule it again. Two concurrent
# Install-ADDSForest calls fight over the same promotion and one of them
# writes FAIL, which stops a provisioning that actually worked.
function Enter-StageLock {
param([string]$Path)
$lock = "$Path.lock"
if (Test-Path $lock) {
$owner = Get-Content $lock -ErrorAction SilentlyContinue
if ($owner -and (Get-Process -Id ([int]$owner) -ErrorAction SilentlyContinue)) {
return $false
}
# The owner is gone (reboot, crash): the lock is stale.
}
Set-Content -Path $lock -Value $PID -Encoding ascii
return $true
}
function Exit-StageLock {
param([string]$Path)
Remove-Item "$Path.lock" -Force -ErrorAction SilentlyContinue
}
function Wait-ForDomain {
param([int]$TimeoutSeconds = 600)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
# Get-ADDomain needs the AD PowerShell module, which only exists on the DC.
# System.DirectoryServices works on any domain-joined machine.
while ((Get-Date) -lt $deadline) {
try {
$null = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
return
} catch {
Start-Sleep -Seconds 10
}
}
throw 'domain services did not come up in time'
}