Add DNS Servers to Azure VNET Using PowerShell

In this blog post, we will add DNS servers to an existing Azure Virtual Network (VNET) using PowerShell.

In the previous posts on the topic, we showed how to create a new VNET with subnets using the Az PowerShell module and how to add subnets to an existing VNET using PowerShell.

Today we are going to take an existing VNET with subnets and add DNS servers to it.

The process to add DNS Servers is the same as adding subnets to an existing VNET, but we DNS we add servers, and we still need to create the VNET object in memory before applying the changes.

Code

In the code below, I am recreating in memory my VNET with all the subnets (Apps, Servers, DMZ), and in line number four, I’m creating the VNET in memory and adding the DNS servers to it ( 172.16.1.1, 172.16.1.2 )

$appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix "172.16.1.0/24"
$serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix "172.16.2.0/24"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"
$updatedvnet = New-AzVirtualNetwork -Name MyVNET -ResourceGroupName vnetlab -Location australiasoutheast -AddressPrefix "172.16.0.0/16" -Subnet $serversubnet, $dmz, $appssubnet -DnsServer 172.16.1.1, 172.16.1.2 -Force:$true
$updatedvnet | Set-AzVirtualNetwork

The last line will set the VNET in Azure modify the existing VNET.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.