{"id":23582,"date":"2024-04-30T10:10:25","date_gmt":"2024-04-30T09:10:25","guid":{"rendered":"https:\/\/aidanfinn.com\/?p=23582"},"modified":"2024-04-30T10:15:03","modified_gmt":"2024-04-30T09:15:03","slug":"script-document-all-azure-private-dns-zones","status":"publish","type":"post","link":"https:\/\/aidanfinn.com\/?p=23582","title":{"rendered":"Script &#8211; Document All Azure Private DNS Zones"},"content":{"rendered":"\n<p>I found myself in a situation where I needed to document <em>a lot<\/em> of Azure Private DNS Zones. I needed the following information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Name of the zone<\/li>\n\n\n\n<li>Subscription name<\/li>\n\n\n\n<li>Resource group name<\/li>\n\n\n\n<li>Name of associated virtual networks<\/li>\n<\/ul>\n\n\n\n<p>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 &#8211; 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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cls\n\n$subs = Get-AzSubscription\n\n$outString = \"| Zone Name | Subscription Name | Resource Group Name | Linked Virtual Network |\"\nWrite-Host $outString\n$outString | Out-File \"dnsLinks.md\"\n\n$outString = \"| --------- | ----------------- | ------------------- | ---------------------- |\"\nWrite-Host $outString\n$outString | Out-File \"dnsLinks.md\" -Append\n\nforeach ($sub in $subs)\n{\n\n    try\n    {\n        $context = Set-AzContext -subscription $sub.id\n   \n        $zones = Get-AzPrivateDnsZone\n\n        foreach ($zone in $zones)\n        {\n            if ($sub.Name -eq \"connectivity\" -or $sub.Name -eq \"connectivity-canary\")\n    {\n        break\n    }\n            try\n            {\n\n                $links = Get-AzPrivateDnsVirtualNetworkLink -ResourceGroupName $zone.ResourceGroupName -ZoneName $zone.Name\n       \n                foreach ($link in $links)\n                {\n                    try\n                    {\n                        $vnetName = ($link.VirtualNetworkId.Split(\"\/\")) | Select-Object -Last 1\n\n                        $outString  = \"| \" + $zone.name + \" | \" + $context.Subscription.Name + \" | \" + $zone.ResourceGroupName + \" | \" + $vnetName + \" |\"\n                        Write-Host $outString\n                        $outString | Out-File \"dnsLinks.md\" -Append\n                    }\n                    catch {}\n                }\n             }\n             catch {}\n        }  \n    }\n    catch {}\n}<\/code><\/pre>\n\n\n\n<p>It probably wouldn&#8217;t take a whole lot more work to add any DNS records if you needed that information too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":18900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[5],"tags":[170,541,87,543,289],"class_list":["post-23582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","tag-azure","tag-azure-private-dns-zones","tag-powershell","tag-script","tag-virtual-network"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/aidanfinn.com\/wp-content\/uploads\/2015\/09\/5315.powershell_2D00_logo.gif_2D00_550x01.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/23582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23582"}],"version-history":[{"count":5,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/23582\/revisions"}],"predecessor-version":[{"id":23587,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/posts\/23582\/revisions\/23587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=\/wp\/v2\/media\/18900"}],"wp:attachment":[{"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aidanfinn.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}