param( [Parameter(Mandatory=$true, HelpMessage="Teilnehmernummer, z.B. 20")] [ValidateRange(1,99)] [int]$TN, [string]$AlteIP = "10.0.10.109" ) <# dc-ip-change.ps1 Umstellung der IP eines Single-DC (win2022) beim Zusammenlegen von SERVER- und LAN-Netz. ACHTUNG: nur auf der VM-Konsole ausfuehren, nicht ueber RDP. Voraussetzung: RSAT-Module DnsServer und ActiveDirectory (auf einem DC per Default vorhanden). #> #region ---------- Parameter: hier anpassen ---------- $NeueIP = "172.17.$((200+$TN)).10" $Praefix = 24 $Gateway = "172.17.$((200+$TN)).1" # OPNsense LAN-Interface $NeuesNetz = "172.17.$((200+$TN)).0/$Praefix" $AltesNetz = "10.0.10.0/24" $Zone = (Get-ADDomain).DNSRoot # z.B. it220.int $Site = "Default-First-Site-Name" #endregion $ErrorActionPreference = "Stop" function Info($t) { Write-Host "[*] $t" -ForegroundColor Cyan } function Ok ($t) { Write-Host "[+] $t" -ForegroundColor Green } function Warn($t) { Write-Host "[!] $t" -ForegroundColor Yellow } Info "Domain: $Zone" Info "Alt: $AlteIP -> Neu: $NeueIP" #region ---------- 1. Interface ermitteln ---------- $Adapter = Get-NetIPAddress -IPAddress $AlteIP -AddressFamily IPv4 -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $Adapter) { throw "Kein Interface mit der IP $AlteIP gefunden. Variable AlteIP pruefen." } $IfIndex = $Adapter.InterfaceIndex Ok "Interface gefunden: Index $IfIndex ($((Get-NetAdapter -InterfaceIndex $IfIndex).Name))" #endregion #region ---------- 2. IP umstellen ---------- Info "Alte Adresse und Default-Route entfernen" Remove-NetIPAddress -InterfaceIndex $IfIndex -AddressFamily IPv4 -Confirm:$false Remove-NetRoute -InterfaceIndex $IfIndex -AddressFamily IPv4 -Confirm:$false ` -ErrorAction SilentlyContinue Info "Neue Adresse setzen" New-NetIPAddress -InterfaceIndex $IfIndex ` -IPAddress $NeueIP ` -PrefixLength $Praefix ` -DefaultGateway $Gateway | Out-Null Info "DNS-Client auf sich selbst zeigen lassen" Set-DnsClientServerAddress -InterfaceIndex $IfIndex -ServerAddresses $NeueIP Ok "IP steht auf $NeueIP" Start-Sleep -Seconds 5 #endregion #region ---------- 3. Reverse-Zone ---------- Info "Reverse-Zone fuer $NeuesNetz anlegen" try { Add-DnsServerPrimaryZone -NetworkId $NeuesNetz -ReplicationScope Domain -DynamicUpdate Secure Ok "Reverse-Zone angelegt" } catch { Warn "Reverse-Zone existiert bereits oder konnte nicht angelegt werden" } $AlteRevZone = "10.0.10.in-addr.arpa" if (Get-DnsServerZone -Name $AlteRevZone -ErrorAction SilentlyContinue) { Info "Alte Reverse-Zone $AlteRevZone entfernen" Remove-DnsServerZone -Name $AlteRevZone -Force Ok "Alte Reverse-Zone entfernt" } #endregion #region ---------- 4. Dienste neu registrieren ---------- Info "DNS-Dienst neu starten" Restart-Service DNS Info "Client-Records neu registrieren" ipconfig /registerdns | Out-Null Start-Sleep -Seconds 5 Info "Netlogon neu starten (registriert die SRV-Records)" Restart-Service Netlogon Start-Sleep -Seconds 15 Ok "Dienste neu gestartet" #endregion #region ---------- 5. Alte A-Records aufraeumen ---------- Info "Leichenrecords mit $AlteIP suchen und entfernen" $Zonen = @($Zone, "_msdcs.$Zone") $Weg = 0 foreach ($z in $Zonen) { if (-not (Get-DnsServerZone -Name $z -ErrorAction SilentlyContinue)) { continue } Get-DnsServerResourceRecord -ZoneName $z -RRType A -ErrorAction SilentlyContinue | Where-Object { $_.RecordData.IPv4Address.IPAddressToString -eq $AlteIP } | ForEach-Object { Write-Host " entferne $($_.HostName) in $z" Remove-DnsServerResourceRecord -ZoneName $z -InputObject $_ -Force $script:Weg++ } } Ok "$Weg alte A-Records entfernt" Warn "Der GUID-CNAME unter _msdcs zeigt auf den Hostnamen und bleibt bewusst stehen." #endregion #region ---------- 6. AD-Standort-Subnetz ---------- Info "Subnetzobjekt in AD-Standorte und -Dienste anpassen" Get-ADReplicationSubnet -Filter "Name -eq '$AltesNetz'" -ErrorAction SilentlyContinue | Remove-ADReplicationSubnet -Confirm:$false if (-not (Get-ADReplicationSubnet -Filter "Name -eq '$NeuesNetz'" -ErrorAction SilentlyContinue)) { New-ADReplicationSubnet -Name $NeuesNetz -Site $Site | Out-Null Ok "Subnetz $NeuesNetz angelegt" } else { Ok "Subnetz $NeuesNetz existiert bereits" } #endregion #region ---------- 7. Kontrolle ---------- Write-Host "" Info "Kontrolle" Write-Host "--- ipconfig ---" -ForegroundColor DarkGray Get-NetIPAddress -InterfaceIndex $IfIndex -AddressFamily IPv4 | Format-Table IPAddress, PrefixLength -AutoSize Write-Host "--- SRV-Record LDAP ---" -ForegroundColor DarkGray Resolve-DnsName -Name "_ldap._tcp.dc._msdcs.$Zone" -Type SRV | Format-Table Name, NameTarget -AutoSize Write-Host "--- nltest ---" -ForegroundColor DarkGray nltest /dsgetdc:$Zone Write-Host "--- dcdiag /test:dns ---" -ForegroundColor DarkGray dcdiag /test:dns /q Write-Host "" Ok "Fertig. Wenn dcdiag stumm bleibt, ist alles sauber." Warn "Nicht vergessen: DHCP-Option 006 auf der OPNsense auf $NeueIP setzen." Warn "Clients (rocky, win11, samba) brauchen den neuen DNS-Server - Lease erneuern." #endregion