Azure PowerShell ARM – Add multiple DNS Servers to Azure RM vNet via PowerShell

As per the Azure online documentation, you can add up to a maximum of 12 DNS servers to a Virtual Network (vNet).

This was easy to do for the classic (v1) vNets via the old Azure management portal. But what if you have an Azure Resource Manager (ARM) based vNet? There is no easy way as you cannot see an ARM based vNet in the classic portal.

The new Azure portal allows you to modify the list of DNS servers for a vNet, but you can add only two DNS entries.

So how do we add more than 2 DNS servers? The answer as usual is, via Azure PowerShell. The following script does exactly that and can be used to add multiple DNS servers to an Azure RM vNet

$subName = "<Subscription Name>"
$rgName = "<Resource Group Name>"
$vNetName = "<vNet Name>"
$DNSIPs = "192.168.1.10", "192.168.1.11", "192.168.1.12" #Modify as necessary. Maximum 12 DNS servers per vNet

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subName

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -name $vNetName

foreach ($IP in $DNSIPs)
{
$vnet.DhcpOptions.DnsServers += $IP
}

Set-AzureRmVirtualNetwork -VirtualNetwork $vnet

In case you wish to modify the existing DNS server values and replace them with new values, use the script below which sets the DNS server to NULL before applying the new list of DNS servers:


$subName = "<Subscription Name>"
$rgName = "<Resource Group Name>"
$vNetName = "<vNet Name>"
$DNSIPs = "192.168.1.10", "192.168.1.11", "192.168.1.12" #Modify as necessary. Maximum 12 DNS servers per vNet

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subName

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -name $vNetName

$vnet.DhcpOptions.DnsServers = $null

foreach ($IP in $DNSIPs)
{
$vnet.DhcpOptions.DnsServers += $IP
}

Set-AzureRmVirtualNetwork -VirtualNetwork $vnet

Advertisements

1 thought on “Azure PowerShell ARM – Add multiple DNS Servers to Azure RM vNet via PowerShell”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s