# Run locally on SGU-DC02 as SYSTEM. The protected manifest owns only explicitly # approved workgroup names; never import arbitrary router clients into AD DNS. [CmdletBinding()] param( [string]$ManifestPath = 'C:\ProgramData\LCI-Admin\Sala-DNS\manifest.json' ) $ErrorActionPreference = 'Stop' Import-Module DnsServer Import-Module ActiveDirectory if ($env:COMPUTERNAME -ne 'SGU-DC02') { throw 'Run this sync on SGU-DC02.' } $manifest = Get-Content -LiteralPath $ManifestPath -Raw | ConvertFrom-Json if ($manifest.Zone -ne 'lci.lasalle.mx' -or $manifest.RouterDns -ne '192.168.55.1') { throw 'Unexpected DNS zone or router in the manifest.' } $results = @() $failed = $false foreach ($entry in $manifest.Records) { $result = [ordered]@{ Name = $entry.Name; Status = ''; IP = $null; Error = $null } try { if ($entry.Name -notmatch '^si-\d{2}$') { throw 'Name is outside the approved SI naming pattern.' } # After domain enrollment, leave DNS management to the machine's secure # registration. Do not fight a domain client or overwrite someone else's A. $adObject = Get-ADComputer -Server $env:COMPUTERNAME -Filter "Name -eq '$($entry.Name)'" if ($adObject) { $result.Status = 'DomainManaged' } else { $records = @(Get-DnsServerResourceRecord -ZoneName $manifest.Zone -Name $entry.Name -ErrorAction SilentlyContinue) if ($records.Count -gt 0 -and ($records.Count -ne 1 -or $records[0].RecordType -ne 'A' -or $records[0].RecordData.IPv4Address.IPAddressToString -ne $entry.LastPublishedIP)) { throw 'DNS record differs from the owned record; refusing to overwrite it.' } $addresses = @(Resolve-DnsName -Name "$($entry.Name)." -Server $manifest.RouterDns -Type A -DnsOnly -QuickTimeout | Where-Object Type -eq A | Select-Object -ExpandProperty IPAddress -Unique) if ($addresses.Count -ne 1 -or $addresses[0] -notmatch '^192\.168\.55\.(\d{1,3})$' -or [int]$Matches[1] -lt 2 -or [int]$Matches[1] -gt 254) { throw 'Router did not return a single host address in Sala Inmersiva.' } $address = $addresses[0] if ($records.Count -eq 0) { Add-DnsServerResourceRecordA -ZoneName $manifest.Zone -Name $entry.Name -IPv4Address $address -TimeToLive ([TimeSpan]::FromMinutes(5)) $result.Status = 'Created' } elseif ($address -ne $entry.LastPublishedIP) { $updated = $records[0].Clone() $updated.RecordData.IPv4Address = [Net.IPAddress]::Parse($address) $updated.TimeToLive = [TimeSpan]::FromMinutes(5) Set-DnsServerResourceRecord -ZoneName $manifest.Zone -OldInputObject $records[0] -NewInputObject $updated $result.Status = 'Updated' } else { $result.Status = 'Current' } $entry.LastPublishedIP = $address $result.IP = $address } } catch { $result.Status = 'Error' $result.Error = $_.Exception.Message $failed = $true } $results += [pscustomobject]$result } # A failed router query preserves the last record. TTL limits client caching, # not record lifetime: operators must review errors instead of assuming freshness. $manifest | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath "$ManifestPath.next" -Encoding UTF8 Move-Item -LiteralPath "$ManifestPath.next" -Destination $ManifestPath -Force [pscustomobject]@{ Time = (Get-Date).ToString('o'); Records = $results } | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath (Join-Path (Split-Path $ManifestPath) 'last-result.json') -Encoding UTF8 $results # When installed, also preserve bare-name answers for workgroup clients on Sala. $shortNameSync = Join-Path $PSScriptRoot 'Sync-SguSalaShortNames.ps1' if (Test-Path -LiteralPath $shortNameSync) { & $shortNameSync -ManifestPath $ManifestPath } if ($failed) { throw 'One or more Sala DNS records could not be synchronized. See last-result.json.' }