question

JohnathanCopeland avatar image
0 Votes"
JohnathanCopeland asked suvasara-MSFT edited

Change vNet DNS servers with Powershell

I have several hundred vNets over several hundred subscriptions.
We have updated the IP of our DNS server that we use in the vNet of each subscription.
I would like to use Powershell to check each vNet in each Subscription for the list of DNS servers and if one of the IP's in the list matches, then change it to the new IP.

Im able to use powershell to get the DNS servers with:
$vnet = Get-AzVirtualNetwork -resourcegroup "ResGroup" -name "VNETNAME"
$vnet.DhcpOptions.DnsServers

I can change the DNSservers in the object with $vnet.DhcpOptions.DnsServers += "IP_address"

But how do I push the change to Azure ?

azure-virtual-networkazure-dns
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @Catalyph ,

to set the DNS server config of a vNet in Azure you can use this script:

 $vNetRGname = "<name of resourcegroup>"
 $vNet = "<name of vNet>"
 $vNet = Get-AzVirtualNetwork -ResourceGroupName $vNetRGname -name $vNet
 # Replace the IPs with your DNS server IPs here
 $array = @("10.0.0.4" "10.0.0.5")
 $newObject = New-Object -type PSObject -Property @{"DnsServers" = $array}
 $vNet.DhcpOptions = $newObject
 $vNet | Set-AzVirtualNetwork


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

suvasara-MSFT avatar image
0 Votes"
suvasara-MSFT answered suvasara-MSFT edited

@Catalyph, to modify the existing DNS server values and replace them with new values, use the below script,

 $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.
      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

Disclaimer: The script sets the DNS server to NULL before applying the new list of DNS servers. I would recommend you to use it in test envoronment first and then deploy to production accordingly.

Source Credit: https://sydsachar.wordpress.com/2016/05/26/azure-powershell-arm-add-multiple-dns-servers-to-azure-rm-vnet-via-powershell/

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.