Script – Document All Azure Private DNS Zones

This post contains a script that will find all Azure Private DNS Zones in a tenant and export information on screen and as markdown in a file.

I found myself in a situation where I needed to document a lot of Azure Private DNS Zones. I needed the following information:

  • Name of the zone
  • Subscription name
  • Resource group name
  • Name of associated virtual networks

The list was long so a copy and paste from the Azure Portal was going to take too long. Instead, I put a few minutes into a script to do the job – it even writes the content as a Markdown table in a .md file, making it super simple to copy/paste the entire piece of text into my documentation in VS Code.

cls

$subs = Get-AzSubscription

$outString = "| Zone Name | Subscription Name | Resource Group Name | Linked Virtual Network |"
Write-Host $outString
$outString | Out-File "dnsLinks.md"

$outString = "| --------- | ----------------- | ------------------- | ---------------------- |"
Write-Host $outString
$outString | Out-File "dnsLinks.md" -Append

foreach ($sub in $subs)
{

    try
    {
        $context = Set-AzContext -subscription $sub.id
   
        $zones = Get-AzPrivateDnsZone

        foreach ($zone in $zones)
        {
            if ($sub.Name -eq "connectivity" -or $sub.Name -eq "connectivity-canary")
    {
        break
    }
            try
            {

                $links = Get-AzPrivateDnsVirtualNetworkLink -ResourceGroupName $zone.ResourceGroupName -ZoneName $zone.Name
       
                foreach ($link in $links)
                {
                    try
                    {
                        $vnetName = ($link.VirtualNetworkId.Split("/")) | Select-Object -Last 1

                        $outString  = "| " + $zone.name + " | " + $context.Subscription.Name + " | " + $zone.ResourceGroupName + " | " + $vnetName + " |"
                        Write-Host $outString
                        $outString | Out-File "dnsLinks.md" -Append
                    }
                    catch {}
                }
             }
             catch {}
        }  
    }
    catch {}
}

It probably wouldn’t take a whole lot more work to add any DNS records if you needed that information too.