59 lines
3.4 KiB
PowerShell
59 lines
3.4 KiB
PowerShell
# Keep bare-name DNS answers available to Sala workgroup clients with no suffix.
|
|
# Each narrowly scoped zone has one A record; normal FQDN records remain the
|
|
# source of truth. No root zone, wildcard, nonsecure updates, or hosts-file edits.
|
|
[CmdletBinding()]
|
|
param([string]$ManifestPath = 'C:\ProgramData\LCI-Admin\Sala-DNS\manifest.json')
|
|
$ErrorActionPreference = 'Stop'
|
|
Import-Module DnsServer
|
|
if ($env:COMPUTERNAME -ne 'SGU-DC02') { throw 'Run this sync on SGU-DC02.' }
|
|
$folder = Split-Path $ManifestPath
|
|
$statePath = Join-Path $folder 'short-names-owned.json'
|
|
$manifest = Get-Content -LiteralPath $ManifestPath -Raw | ConvertFrom-Json
|
|
if ($manifest.Zone -ne 'lci.lasalle.mx') { throw 'Unexpected source zone.' }
|
|
$owned = @{}
|
|
if (Test-Path -LiteralPath $statePath) {
|
|
$state = Get-Content -LiteralPath $statePath -Raw | ConvertFrom-Json
|
|
foreach ($property in $state.PSObject.Properties) { $owned[$property.Name] = $property.Value }
|
|
}
|
|
$results = @()
|
|
$failed = $false
|
|
foreach ($name in (@('sgu-dc02') + @($manifest.Records.Name) | Sort-Object -Unique)) {
|
|
try {
|
|
if ($name -notmatch '^(sgu-dc02|si-\d{2})$') { throw 'Name outside approved scope.' }
|
|
$source = @(Get-DnsServerResourceRecord -ZoneName $manifest.Zone -Name $name -RRType A)
|
|
if ($source.Count -ne 1) { throw 'Expected one source A record.' }
|
|
$ip = $source[0].RecordData.IPv4Address.IPAddressToString
|
|
if ($ip -notmatch '^192\.168\.(54|55)\.\d{1,3}$') { throw 'Source IP outside managed networks.' }
|
|
$zone = Get-DnsServerZone | Where-Object ZoneName -eq $name
|
|
if (-not $zone) {
|
|
Add-DnsServerPrimaryZone -Name $name -ReplicationScope Domain -DynamicUpdate None
|
|
$owned[$name] = $ip
|
|
} elseif (-not $owned.ContainsKey($name) -or -not $zone.IsDsIntegrated -or $zone.DynamicUpdate -ne 'None') {
|
|
throw 'Existing bare-name zone is not owned by this sync.'
|
|
}
|
|
$records = @(Get-DnsServerResourceRecord -ZoneName $name -Name '@' | Where-Object RecordType -notin @('SOA','NS'))
|
|
if ($records.Count -eq 0) {
|
|
Add-DnsServerResourceRecordA -ZoneName $name -Name '@' -IPv4Address $ip -TimeToLive ([TimeSpan]::FromMinutes(5))
|
|
} elseif ($records.Count -ne 1 -or $records[0].RecordType -ne 'A' -or
|
|
$records[0].RecordData.IPv4Address.IPAddressToString -ne $owned[$name]) {
|
|
throw 'Bare-name record differs from the owned record; refusing to overwrite it.'
|
|
} elseif ($ip -ne $owned[$name]) {
|
|
$updated = $records[0].Clone()
|
|
$updated.RecordData.IPv4Address = [Net.IPAddress]::Parse($ip)
|
|
$updated.TimeToLive = [TimeSpan]::FromMinutes(5)
|
|
Set-DnsServerResourceRecord -ZoneName $name -OldInputObject $records[0] -NewInputObject $updated
|
|
}
|
|
$owned[$name] = $ip
|
|
$results += [pscustomobject]@{Name=$name;IP=$ip;Status='Current';Error=$null}
|
|
} catch {
|
|
$failed = $true
|
|
$results += [pscustomobject]@{Name=$name;IP=$null;Status='Error';Error=$_.Exception.Message}
|
|
}
|
|
}
|
|
$owned | ConvertTo-Json | Set-Content -LiteralPath "$statePath.next" -Encoding UTF8
|
|
Move-Item -LiteralPath "$statePath.next" -Destination $statePath -Force
|
|
[pscustomobject]@{Time=(Get-Date).ToString('o');Records=$results} | ConvertTo-Json -Depth 5 |
|
|
Set-Content -LiteralPath (Join-Path $folder 'short-names-result.json') -Encoding UTF8
|
|
$results
|
|
if ($failed) { throw 'Some bare-name records could not be synchronized. See short-names-result.json.' }
|