Public/Backup-DNSServerZones.ps1

Function Backup-DNSServerZones
{
   param(
      [Parameter(Mandatory)][string]$ComputerName
   )

   # Before backing up the file we need to flush changes in memory to the file.
   # GUI - Right click zone / Update server data file
   # Use "dnscmd /writebackfiles" to flush all zones to disk.
   # Use "dnscmd /zonewriteback" to flush a single zone to disk.
   Dnscmd $ComputerName /writebackfiles

   $Zones = Get-DnsServerZone -ComputerName $ComputerName |
   Where-Object { $_.ZoneType -eq 'Primary' -and $_.IsAutoCreated -eq $false } |
   Select-Object -ExpandProperty ZoneName

   ForEach ($Zone in $Zones)
   {
      $Zone

      # Make a backup copy of the CONTOSO.com.dns zone file
      Dnscmd $ComputerName /ZoneExport $Zone $($Zone + "_backup_$(Get-Date -UFormat %Y%m%d%H%M%S).dns")

      # Dump a CSV report of all records for archive and TTL documentation.
      $ZoneRecords = Get-WMIObject -ComputerName $ComputerName `
         -Namespace root\MicrosoftDNS -Class MicrosoftDNS_ResourceRecord `
         -Filter "DomainName='$Zone'"

      $ZoneRecords |
      Select-Object ContainerName, DnsServerName, DomainName, IPAddress, `
         OwnerName, RecordClass, RecordData, TextRepresentation, `
         Timestamp, TTL |
      Export-Csv "$($Zone)_backup_$(Get-Date -UFormat %Y%m%d%H%M%S).csv" -NoTypeInformation

   }

   # Verify backup files exists
   Get-ChildItem -Path "\\$ComputerName\admin$\system32\dns\"
}