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

/ November 2nd, 2016/ Posted in Random / No Comments »
$dnsServer = "baldns1"
$oldDomain = "mytest.baldanza.org"
$newDomain = "mytest.baldanza.org"
$dynamicUpdate = "None"
$responsiblePerson = "hostmaster.baldanza.org."
$notifySettings = "NotifyServers"
$transferType = "TransferToSecureServers"
$secondaryServers = "10.23.0.4","10.23.0.5","192.20.0.4","192.0.5"


#DONT TOUCH BELOW
$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"
}

Leave a Reply

Name required

Please Submit Answer *