Move A records from subdomain to a new Primary Zone non Active Directory Integrated

2016-11-02 / Random / 0 Comments
$dnsServer = "baldns1"
$oldDomain = ""
$newDomain = ""
$dynamicUpdate = "None"
$responsiblePerson = ""
$notifySettings = "NotifyServers"
$transferType = "TransferToSecureServers"
$secondaryServers = "","","","192.0.5"

$records = Get-WmiObject -ComputerName $dnsServer -Namespace root\microsoftDNS -Query ("select * from MicrosoftDNS_AType where DomainName = ""$oldDomain""")
$foundZone = Get-DnsServerZone -ComputerName $dnsServer -Name $newDomain -ErrorAction SilentlyContinue
if ($foundZone -eq $null) {
    Write-Output "Creating zone $newDomain..."
    Add-DnsServerPrimaryZone -ComputerName $dnsServer -Name "$newDomain" -ZoneFile "$newDomain.dns" -ResponsiblePerson $responsiblePerson -DynamicUpdate $dynamicUpdate
    Set-DnsServerPrimaryZone -ComputerName $dnsServer -Name $newDomain -Notify $notifySettings -NotifyServers $secondaryServers -SecureSecondaries $transferType -SecondaryServers $secondaryServers -PassThru
    $foundZone = Get-DnsServerZone -ComputerName $dnsServer -Name $newDomain -ErrorAction SilentlyContinue
} else {
    Write-Output "Zone: $newDomain already created"
if ($foundZone -ne $null) {
    Write-Output "Copying records from $oldDomain to $newDomain"
    foreach ($record in $records) {
        $hostname = $record.OwnerName.Replace("."+$record.DomainName, "").ToString();
        $ipAddr = $($record.IPAddress.ToString());
        $ipTTL = $($record.TTL.ToString());
        $ipTTL = [timespan]::fromseconds($ipTTL)
        try {
            Write-Output "Adding..."
            Write-Output "Hostname: $hostname"
            Write-Output "IP: $ipAddr"
            Write-Output "TTL: $ipTTL"
            Add-DnsServerResourceRecordA -ComputerName $dnsServer -Name "$hostname" -ZoneName "$newDomain" -AllowUpdateAny -IPv4Address "$ipAddr" -TimeToLive $ipTTL
        } catch {
            Write-Output "Unable to add: $hostname - $ipAddr"
} else {
    Write-Host "Unable to copying records because $newDomain does not exist"
Read More