Reading Time: 3 minutes
Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

Recently I have been creating some CNAME records on one of my domain registers DNS portal. They had recently migrated to a new portal to make things “easier”. This has caused a lot of issues, the last issue I had was being unable to create CNAMES with a in them. There was no error showing in their portal, but DNS Zone was not being updated. At one point with this provider, I even lost DNS altogether.

Due to all of the issues especially not being able to create CNAMES with a I decided to migrate my DNS to Microsoft Azure. The process of creating the DNS Zone in Azure is very easy. I may even create a blog post on it one day. Unfortunately, for me, my old provider did not allow zone exports so I had to just copy and paste the records from their online portal to an excel file. Now, I could have added each record manually, I did for the MX, TXT etc, but for the A  and CNAMES, as I had a lot of them, I wanted to use PowerShell to upload them from a CSV file. Below you will find the PowerShell commands I used to accomplish this.

Adding Multiple A Records

For this to work you will need to create a CSV file with the headings Name, RecordType, ZoneName, ResourceGroupName, TTL and Value. Your file should look something like the one below.

You will notice the file is called arecords.csv. Make sure you save the file in a safe place.

Now we can use PowerShell to add the files. You will need the Azure PowerShell module installed for this to work.

In your elevated PowerShell window login to your Azure subscription using the Login-AzureRMAccount cmdlet.

Note: If you have multiple subscriptions make sure you have selected the subscription that your DNS zone lives in.

It’s time for the bit of code to import the DNS entries. Make sure you change the code so it points to the location of the .csv you created earlier.

#Bulk Import A Records
Import-CSV -Path "C:\scripts\arecords.csv" | ForEach-Object { New-AzureRmDnsRecordSet -Name $_.Name -RecordType $_.RecordType -ZoneName $_.ZoneName -ResourceGroupName $_.ResourceGroupName-Ttl $_.TTL -DnsRecords (New-AzureRmDnsRecordConfig -IPv4Address $_.Value) }
view raw Bulkadd_AZ_DNS.PS1 hosted with ❤ by GitHub

If you look in the Azure Portal you will see the entries there too.

Adding Multiple CNAME Records

To add multiple CNAMES we can use the Same CSV file as a base, you just need to populate it with the CNAME records. It should look something like this.

You should notice this file is called cnamerecords.csv save it in the same place as the A record if you like.

Now back into the elevated PowerShell window. If you have not logged in to your Azure subscription read above on how to do that. To add the multiple CNAMES you can use the following PowerShell command, remember to change the path to the location of your CSV file.

#Bulk Import CNAME Records
Import-CSV -Path "C:\scripts\cnamerecords.csv" | ForEach-Object { New-AzureRmDnsRecordSet -Name $_.Name -RecordType $_.RecordType -ZoneName $_.ZoneName -ResourceGroupName $_.ResourceGroupName-Ttl $_.TTL -DnsRecords (New-AzureRmDnsRecordConfig -Cname $_.Value) }
view raw Bulkadd_AZ_DNS.PS1 hosted with ❤ by GitHub

Again if you look in the Azure Portal you will see the entries there too.

How easy was that? It saved me a lot of time. I hope it saves you some time too. If you have any questions leave a comment below or reach out using the usual methods.

Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email
Categories: Azure

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

1 Comment

Leonardo Penha Mesquita · August 23, 2018 at 1:32 am

Great post! Too bad it wasn’t posted a few weeks ago, when I need to add some registers, and had to do it one by one, hehehe.
Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

I agree