How to manage DNS Zones using PowerShell

Set up Azure PowerShell for Azure DNS

Before you begin

Verify that you have the following items before beginning your configuration.

  • An Azure subscription. If you don’t already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free account.
  • You need to install the latest version of the Azure Resource Manager PowerShell cmdlets. For more information, see How to install and configure Azure PowerShell.

In addition, to use Private Zones (Public Preview), you need to ensure you have the below PowerShell modules and versions.

  • AzureRM.Dns – version 4.1.0 or above
  • AzureRM.Network – version 5.4.0 or above

Find-Module -Name AzureRM.Dns

Find-Module -Name AzureRM.Network

The output of the above commands need to show that the version of AzureRM.Dns is 4.1.0 or higher version, and for AzureRM.Network is 5.4.0 or higher version.

In case your system has earlier versions, you can either install the latest version of Azure PowerShell, or download and install the above modules from the PowerShell Gallery, using the links above next to the Module versions. You can then install them using the below commands. Both the modules are required and are fully backwards compatible.

Install-Module -Name AzureRM.Dns -Force

Install-Module -Name AzureRM.Network -Force

Sign in to your Azure account

Open your PowerShell console and connect to your account. For more information, see Using PowerShell with Resource Manager.

Connect-AzureRmAccount

 

Select the subscription

Check the subscriptions for the account.

Get-AzureRmSubscription

Choose which of your Azure subscriptions to use.

Select-AzureRmSubscription -SubscriptionName “your_subscription_name”

Create a resource group

Azure Resource Manager requires that all resource groups specify a location. This location is used as the default location for resources in that resource group. However, because all DNS resources are global, not regional, the choice of resource group location has no impact on Azure DNS.

You can skip this step if you are using an existing resource group.

New-AzureRmResourceGroup -Name MyAzureResourceGroup -location “East US”

Register resource provider

The Azure DNS service is managed by the Microsoft.Network resource provider. Your Azure subscription must be registered to use this resource provider before you can use Azure DNS. This is a one-time operation for each subscription.

Register-AzureRmResourceProvider -ProviderNamespace Microsoft.Network

Create a DNS zone

A DNS zone is created by using the New-AzureRmDnsZone cmdlet.

The following example creates a DNS zone called contoso.com in the resource group called MyResourceGroup:

New-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup

The following example shows how to create a DNS zone with two Azure Resource Manager tagsproject = demo and env = test:

New-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup -Tag @{ project=”demo”; env=”test” }

Azure DNS now also supports private DNS zones (currently in public preview). To learn more about private DNS zones, see Using Azure DNS for private domains. For an example of how to create a private DNS zone, see Get started with Azure DNS private zones using PowerShell.

Get a DNS zone

To retrieve a DNS zone, use the Get-AzureRmDnsZone cmdlet. This operation returns a DNS zone object corresponding to an existing zone in Azure DNS. The object contains data about the zone (such as the number of record sets), but does not contain the record sets themselves (see Get-AzureRmDnsRecordSet).

Get-AzureRmDnsZone -Name chirkut.com –ResourceGroupName MyAzureResourceGroup

Name : chirkut.com
ResourceGroupName : myresourcegroup
Etag : 00000003-0000-0000-8ec2-f4879750d201
Tags : {project, env}
NameServers : {ns1-01.azure-dns.com., ns2-01.azure-dns.net., ns3-01.azure-dns.org.,
ns4-01.azure-dns.info.}
NumberOfRecordSets : 2
MaxNumberOfRecordSets : 5000

 

List DNS zones

By omitting the zone name from Get-AzureRmDnsZone, you can enumerate all zones in a resource group. This operation returns an array of zone objects.

$zoneList = Get-AzureRmDnsZone -ResourceGroupName MyAzureResourceGroup

By omitting both the zone name and the resource group name from Get-AzureRmDnsZone, you can enumerate all zones in the Azure subscription.

$zoneList = Get-AzureRmDnsZone

Update a DNS zone

Changes to a DNS zone resource can be made by using Set-AzureRmDnsZone. This cmdlet does not update any of the DNS record sets within the zone (see How to Manage DNS records). It’s only used to update properties of the zone resource itself. The writable zone properties are currently limited to the Azure Resource Manager ‘tags’ for the zone resource.

Use one of the following two ways to update a DNS zone:

Specify the zone using the zone name and resource group

This approach replaces the existing zone tags with the values specified.

Set-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup -Tag @{ project=”demo”; env=”test” }

Specify the zone using a $zone object

This approach retrieves the existing zone object, modifies the tags, and then commits the changes. In this way, existing tags can be preserved.

# Get the zone object
$zone = Get-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup

# Remove an existing tag
$zone.Tags.Remove(“project”)

# Add a new tag
$zone.Tags.Add(“status”,”approved”)

# Commit changes
Set-AzureRmDnsZone -Zone $zone

When using Set-AzureRmDnsZone with a $zone object, Etag checks are used to ensure concurrent changes are not overwritten. You can use the optional -Overwrite switch to suppress these checks.

Delete a DNS Zone

DNS zones can be deleted using the Remove-AzureRmDnsZone cmdlet.

Use one of the following two ways to delete a DNS zone:

Specify the zone using the zone name and resource group name

Remove-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup

Specify the zone using a $zone object

You can specify the zone to be deleted using a $zone object returned by Get-AzureRmDnsZone.

$zone = Get-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup
Remove-AzureRmDnsZone -Zone $zone

The zone object can also be piped instead of being passed as a parameter:

Get-AzureRmDnsZone -Name chirkut.com -ResourceGroupName MyAzureResourceGroup | Remove-AzureRmDnsZone

As with Set-AzureRmDnsZone, specifying the zone using a $zone object enables Etag checks to ensure concurrent changes are not deleted. Use the -Overwrite switch to suppress these checks.

Confirmation prompts

The New-AzureRmDnsZoneSet-AzureRmDnsZone, and Remove-AzureRmDnsZone cmdlets all support confirmation prompts.

Both New-AzureRmDnsZone and Set-AzureRmDnsZone prompt for confirmation if the $ConfirmPreference PowerShell preference variable has a value of Medium or lower. Due to the potentially high impact of deleting a DNS zone, the Remove-AzureRmDnsZonecmdlet prompts for confirmation if the $ConfirmPreference PowerShell variable has any value other than None.

Since the default value for $ConfirmPreference is High, only Remove-AzureRmDnsZone prompts for confirmation by default.

You can override the current $ConfirmPreference setting using the -Confirm parameter. If you specify -Confirm or -Confirm:$True, the cmdlet prompts you for confirmation before it runs. If you specify -Confirm:$False , the cmdlet does not prompt you for confirmation.

For more information about -Confirm and $ConfirmPreference, see About Preference Variables.

Leave a Reply

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