Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to manage DNS records in the Azure Private Zone

·801 words·4 mins· 100 views · 5 likes ·
Add-AzPrivateDnsRecordConfig Azure CLI Azure Cloud Shell Azure PowerShell

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>

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>

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

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"

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

Set the variables>

Set the variables #

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

$resourceGroupName = "RG-DEMO-NE"
$priveZoneName = "private.jorgebernhardt.local"
Create DNS records>

Create DNS records #

To create a record set containing multiple records, use the 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

New-AzPrivateDnsRecordConfig
Instead of creating 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")

Azure DNS Records

Update DNS records>

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

Set-AzPrivateDnsRecordSet
To update the current record set, use the following command.

$RecordSet = Get-AzPrivateDnsRecordSet `
  -Name "text" `
  -ResourceGroupName $resourceGroupName `
  -ZoneName $priveZoneName `
  -RecordType TXT `

$RecordSet.Ttl = 4800
Set-AzPrivateDnsRecordSet `
  -RecordSet $RecordSet

Azure DNS Records

List DNS records>

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

Get-AzPrivateDnsRecordSet

Delete DNS records>

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

Remove-AzPrivateDnsRecordSet
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

Azure DNS Records

Azure CLI Workaround>

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>

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

Azure DNS Records

Update DNS records>

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

Azure DNS Records
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

Azure DNS Records

List DNS records>

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

Azure DNS Records

Delete DNS records>

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

Azure DNS Records
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

Azure DNS Records
Thanks for reading my post. I hope you find it helpful.

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

You can learn more about Azure Private DNS zone here.