How to Easily Migrate your Entire On-Prem DNS to Azure

So as with many companies we’re shifting to a Cloud environment from aging physical systems, as part of this I came across a requirement to migrate our entire On-Premise DNS System which is hosted on a Windows Server into Azure.

I wasn’t about to start migrating a few hundred individual DNS Zones in this case as that doesn’t sound like a whole lot of fun when it’s a seemingly repetitive task that I could instead script up so that’s what I did.

The instructions and code below are meant to get you in the right direction, the script is not necessarily production ready and could be improved greatly, if you take the time to do so please comment with any alterations you make and I’ll include them in this article.

Instructions

  1. Access the server hosting the Microsoft DNS.
  2. Navigate to “C:\Windows\System32\dns” which hosts all of the DNS files for every zone.
  3. Copy these files to a local folder, Do not run against the source as it will modify the files.
  4. Run the PowerShell script below making sure to follow the preceding instructions.
  5. You should then find all of the new DNS Zones in the specified resource group.

PowerShell

  • Make sure to replace the $ResourceGroup value with the resource group name that the DNS Zones will be created in.
  • Replace $Subscription with the subscription the resource group is in.
  • Replace $Path with the directory containing all of the .dns files
  • In the section with the comment “Remove the NS info” you may need to add further replace commands to remove any NS servers found in the DNS files, replace can handle Regex. The Azure Import command will throw an error if the NS records point to a domain not yet managed by Azure.
# Get the right Subscription
$ResourceGroup = "Resource-Name"
$Subscription = "Subscription-Name"
$Path = "PATH"

az account set --subscription $Subscription

# Get all of the DNS files
$DNSFiles = Get-ChildItem -Path $Path -Filter *.dns | select name, FullName

$Count = 1
foreach($file in $DNSFiles) {
    $name = ($file.Name).Substring(0, ($file.Name).Length-4)

    Write-Progress -Id 0 -Activity "Prcoessing DNS" -Status "$Count of $($DNSFiles.Count)" -PercentComplete (($Count / $DNSFiles.Count) * 100)

    # Remove the NS info
    ((Get-Content -path $file.FullName -Raw) -replace "@                       NS	*.contoso.com.") | Set-Content -path $file.FullName
    ((Get-Content -path $file.FullName -Raw) -replace "*.constoso.com.       .+") | Set-Content -path $file.FullName

    # Import the DNS zone file
    az network dns zone import -g $ResourceGroup -n $name -f $file.FullName

    $Count++
}
Write-Progress -Id 0 -Activity " " -Status " " -Completed

3 thoughts on “How to Easily Migrate your Entire On-Prem DNS to Azure

  1. How did you go from there? Our VMs still use the on-prem DNS even after linking the Azure private zone to our vnet, choosing it as dns master and rebooting all vms

    Like

    1. Hey Rene, it’s been a while since I had to handle this but I believe we had quite a few subdomains and that the script I provided above managed to put them into an importable format.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.