param(
[Parameter(Mandatory=$true)][string]$RecordFqdn, # es. pippo.my3cx.it
[Parameter(Mandatory=$true)][string]$NewIP, # es. 192.168.2.150 (failover) o 192.168.1.150 (primario)
[string[]]$DnsServers = @(
"dns-sede1.mydomain.local",
"dns-sede2.mydomain.local",
"dns-sede3.mydomain.local",
"dns-sede4.mydomain.local",
"dns-sede5.mydomain.local",
"dns-sede6.mydomain.local"
),
[timespan]$Ttl = ([timespan]::FromMinutes(1)),
[string]$LogPath = "C:\Scripts\logs\Update-3CX-SplitDNS.log"
)
function Write-Log { param([string]$msg)
$stamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
"$stamp $msg" | Out-File -FilePath $LogPath -Append -Encoding UTF8
}
# parsing zona/nome
if ($RecordFqdn -notmatch '^[^.]+\..+$') { throw "RecordFqdn non valido: $RecordFqdn" }
$parts = $RecordFqdn.Split('.',2)
$RecordName = $parts[0]
$ZoneName = $parts[1]
Write-Log "=== Start update: $RecordFqdn -> $NewIP ==="
foreach ($server in $DnsServers) {
try {
Write-Log "[$server] Rimozione A esistente per $RecordName.$ZoneName"
Remove-DnsServerResourceRecord -ComputerName $server -ZoneName $ZoneName -RRType "A" -Name $RecordName -Force -ErrorAction SilentlyContinue
Write-Log "[$server] Creazione A ($NewIP) TTL $($Ttl.TotalSeconds)s"
Add-DnsServerResourceRecordA -ComputerName $server -ZoneName $ZoneName -Name $RecordName -IPv4Address $NewIP -TimeToLive $Ttl
# verifica
Start-Sleep -Milliseconds 300
$resp = Resolve-DnsName -Server $server -Name $RecordFqdn -Type A -ErrorAction Stop
$ips = ($resp | Where-Object {$_.Type -eq 'A'}).IPAddress -join ','
Write-Log "[$server] Verifica OK: $RecordFqdn = $ips"
}
catch {
Write-Log "[$server] ERRORE: $($_.Exception.Message)"
}
}
Write-Log "=== Fine update ==="