Below is the script to export the zone file from Azure DNS.



#Login to Azure using CLI with Username and Password
az login -u <Put Username Here> -p <Put Password Here>

#Select Subscription 

az account set -s <Put Subscription Name here>

#Set date format to create folder automatically with the date to export zone files

$date = Get-Date

$dateFormat = $date.ToString("yyyy-MM-dd")

New-Item -ItemType directory -Path "D\AzureDNSBackup\$dateFormat"

#Use below command to export the each zone file one at a time

az network dns zone export -g "Put Resource Group Name Here" -n "Put zone name here" -f "D:\AzureDNSBackup\$dateFormat\ZoneFileName.txt"

#Export the list of zone files from folder to a file

Get-ChildItem -Path "D:\AzureDNSBackup\$dateFormat\*.txt" | out-file "D:\AzureDNSBackup\$dateFormat\Zone_List.txt”

#Send the zone file list in email for the backup confirmation

$filename = “D:\AzureDNSBackup\$dateFormat\Zone_List.txt”

$smtpServer = “relay Server Name/IP”
$msg = new-object Net.Mail.MailMessage

$att = new-object Net.Mail.Attachment($filename)

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “Sender Email Address here”

$msg.To.Add(“Put Recipeint Email Address Here”)

$msg.Subject = “Daily Azure DNS Zone Backup - $((Get-Date).ToShortDateString())”

$msg.Body = “Daily Azure DNS Zone Backup done to D drive AzureDNSBackup folder on server "ServerName", Backup Zone List Attached”

$msg.Attachments.Add($att)$msg.IsBodyHTML = $true