In a previous post, I showed you how to create a private DNS zone in Azure using PowerShell and Azure CLI. Today I will show you how to manage the DNS records of that area using PowerShell and Azure CLI.

Prerequisites

This tutorial assumes that you already have a Microsoft Azure account configured.

You already have a private DNS zone created and properly configured. If you want to know how to create it, see this link

Azure PowerShell Workaround

If you want to know how to install the PowerShell Azure module on your machine, check out this link.

The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount 1 2 3 Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account.

If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription Select-AzSubscription -Subscription "My Subscription" 1 2 3 4 Get-AzSubscription Select -AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables

Here, we define the characteristics of our environment and the resource’s properties.

$resourceGroupName = "RG-DEMO-NE" $priveZoneName = "private.jorgebernhardt.local" 1 2 3 4 $resourceGroupName = "RG-DEMO-NE" $priveZoneName = "private.jorgebernhardt.local"

Create DNS records

To create a record set containing multiple records, use New-AzPrivateDnsRecordConfig cmdlet with the following syntax.

$Records = @() $Records += New-AzPrivateDnsRecordConfig -IPv4Address 192.168.1.4 $Records += New-AzPrivateDnsRecordConfig -IPv4Address 192.168.1.5 $RecordSet = New-AzPrivateDnsRecordSet -Name "www" ` -RecordType A ` -ResourceGroupName $resourceGroupName ` -TTL 3600 ` -ZoneName $priveZoneName ` -PrivateDnsRecords $Records 1 2 3 4 5 6 7 8 9 10 11 $Records = @ ( ) $Records += New-AzPrivateDnsRecordConfig -IPv4Address 192 . 168 . 1 . 4 $Records += New-AzPrivateDnsRecordConfig -IPv4Address 192 . 168 . 1 . 5 $RecordSet = New-AzPrivateDnsRecordSet -Name "www" ` -RecordType A ` -ResourceGroupName $resourceGroupName ` -TTL 3600 ` -ZoneName $priveZoneName ` -PrivateDnsRecords $Records

instead to create a record with a single value, use the New-AzPrivateDnsRecordSet cmdlet with the following syntax.

New-AzPrivateDnsRecordSet -Name "text" ` -RecordType TXT ` -ResourceGroupName $resourceGroupName ` -TTL 3600 ` -ZoneName $priveZoneName ` -PrivateDnsRecords (New-AzPrivateDnsRecordConfig -Value "This is a TXT Record of Jorgebernhardt.local zone") 1 2 3 4 5 6 7 8 New-AzPrivateDnsRecordSet -Name "text" ` -RecordType TXT ` -ResourceGroupName $resourceGroupName ` -TTL 3600 ` -ZoneName $priveZoneName ` -PrivateDnsRecords ( New-AzPrivateDnsRecordConfig -Value "This is a TXT Record of Jorgebernhardt.local zone" )

Update DNS records

if you want to add a record to an existing record set in a private DNS zone, use the following commands.

$RecordSet = Get-AzPrivateDnsRecordSet -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -Name "www" ` -RecordType A Add-AzPrivateDnsRecordConfig -RecordSet $RecordSet ` -Ipv4Address 192.168.1.6 Set-AzPrivateDnsRecordSet -RecordSet $RecordSet 1 2 3 4 5 6 7 8 9 $RecordSet = Get-AzPrivateDnsRecordSet -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -Name "www" ` -RecordType A Add-AzPrivateDnsRecordConfig -RecordSet $RecordSet ` -Ipv4Address 192 . 168 . 1 . 6 Set -AzPrivateDnsRecordSet -RecordSet $RecordSet

To update the existing record set, use the following command.

$RecordSet = Get-AzPrivateDnsRecordSet -Name "text" ` -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType TXT ` $RecordSet.Ttl = 4800 Set-AzPrivateDnsRecordSet -RecordSet $RecordSet 1 2 3 4 5 6 7 8 9 $RecordSet = Get-AzPrivateDnsRecordSet -Name "text" ` -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType TXT ` $RecordSet . Ttl = 4800 Set -AzPrivateDnsRecordSet -RecordSet $RecordSet

List DNS records

To list all records in the private zone, use the cmdlet Get-AzPrivateDnsRecordSet with the following syntax.

Get-AzPrivateDnsRecordSet -ZoneName $priveZoneName ` -ResourceGroupName $resourceGroupName ` | Select-Object Name, Recordtype, Records | Sort-Object RecordType 1 2 3 4 5 Get-AzPrivateDnsRecordSet -ZoneName $priveZoneName ` -ResourceGroupName $resourceGroupName ` | Select-Object Name , Recordtype , Records | Sort-Object RecordType

Delete DNS records

To delete a specific record, use the Remove-AzPrivateDnsRecordSet cmdlet with the following syntax

Remove-AzPrivateDnsRecordSet -Name "www" ` -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType A 1 2 3 4 5 6 Remove-AzPrivateDnsRecordSet -Name "www" ` -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType A

If instead, you want to delete all records of the same type, use the following commands.

Get-AzPrivateDnsRecordSet -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType A ` | Remove-AzPrivateDnsRecordSet 1 2 3 4 5 6 Get-AzPrivateDnsRecordSet -ResourceGroupName $resourceGroupName ` -ZoneName $priveZoneName ` -RecordType A ` | Remove-AzPrivateDnsRecordSet

Azure CLI Workaround

In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link.

Create DNS records

To create an A record, use the following commands.

az network private-dns record-set a add-record \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -n www \ -a 192.168.1.4 1 2 3 4 5 6 7 az network private - dns record - set a add - record \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - n www \ - a 192.168.1.4

Update DNS records

If you want to update the existing record set, use the following command.

az network private-dns record-set a update \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -n www \ --set ttl=4800 1 2 3 4 5 6 7 az network private - dns record - set a update \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - n www \ -- set ttl = 4800

if instead, you want to add a record to an existing record set, use the following command

az network private-dns record-set a add-record \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -n www \ -a 192.168.1.5 1 2 3 4 5 6 7 az network private - dns record - set a add - record \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - n www \ - a 192.168.1.5

List DNS records

To list all records in the private zone, use the following command.

az network private-dns record-set list \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -o table 1 2 3 4 5 6 az network private - dns record - set list \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - o table

Delete DNS records

To only remove a specific record from a record set, use the following command.

az network private-dns record-set a remove-record \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -n www \ -a 192.168.1.4 1 2 3 4 5 6 7 az network private - dns record - set a remove - record \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - n www \ - a 192.168.1.4

If you want to delete a record set and all associated records, use the following command.

az network private-dns record-set a delete \ -g RG-DEMO-NE \ -z private.jorgebernhardt.local \ -n www \ -y 1 2 3 4 5 6 7 az network private - dns record - set a delete \ - g RG - DEMO - NE \ - z private .jorgebernhardt .local \ - n www \ - y

Thanks for reading my post. I hope you find it useful.

You can learn more about Azure Private DNS zone here: https://docs.microsoft.com/en-us/azure/dns/private-dns-overview

Was this article useful? Support my work!