Change vNet DNS servers with Powershell

Johnathan Copeland 41 Reputation points
2021-04-20T20:02:01.857+00:00

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 DNS
Azure DNS
An Azure service that enables hosting Domain Name System (DNS) domains in Azure.
707 questions
Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,574 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 113.4K Reputation points MVP
    2021-04-20T20:26:39.357+00:00

    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

    3 people found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. suvasara-MSFT 10,056 Reputation points
    2021-04-20T20:35:06.893+00:00

    @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/

    0 comments No comments

  2. Rajesh Swarnkar 881 Reputation points
    2024-11-26T10:54:11.67+00:00

    I updated the script with loops:

    Import-module Az.Resources
    Import-module Az.Compute
    $path = "vnet-list.csv" 
    $csv = Import-Csv -path $path
    $New_DNS_Servers = @("10.10.10.10","10.20.30.40")
    $New_CustomDNS_Obj = New-Object -type PSObject -Property @{"DnsServers" = $New_DNS_Servers}
    # Iterate through each subscription:
    foreach ($line in $csv) { 
        $properties = $line | Get-Member -MemberType Properties
        $rg_name = $line | Select -ExpandProperty $properties[0].Name
        $subscription_id = $line | Select -ExpandProperty $properties[1].Name
        $vnet_name = $line | Select -ExpandProperty $properties[2].Name
    	Set-AzContext -SubscriptionId $subscription_id | Out-null
    	$VNet_Obj = Get-AzVirtualNetwork -ResourceGroupName $rg_name -name $vnet_name
    	$VNet_Obj.DhcpOptions = $New_CustomDNS_Obj
    	$result = Set-AzVirtualNetwork -VirtualNetwork $VNet_Obj
        if ($result -eq "") {
    		Write-Host "Error while updating Custom DNS-Server for $($vnet_name)"
    	} 
    	else {
    		Write-Host "Updated Custom DNS-Server for $($vnet_name)"
    	}
    	$VNet_Obj = ""
    } 
     
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.