Powershell Script :How to Backup DNS Zones

# —Note: A newer version of the script is available here

 

Hello World,

Today, I want to post a short powershell script.  I’ve been asked if it was possible to create a powershell script to perform backup of DNS zones.  First thing that came into my mind was to use the dnscmd.exe command line that can be used to automate a lot of DNS configuration options.  After some times, we have decided to check if it was not possible to create a powershell script that would perform the backup of the DNS zones.

Basic Powershell Script for Backing Up DNS Zones

The script provide basically the logic that has been used.  You can use this script as a starting point.

#————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.3
# Date : October 2011
# Created by Griffon
#
# DO NOT DELETE THIS SECTION – PLEASE
#
#———————————————————————————#

#— Here we simply use the environment#variable to get the name of the server
#— You can connect to a remote server if you want to.
#————————————————————————–

$DNSSERVER=gc env:computername

#—-  DEFINE WHERE TO STORE DNS BACKUP FILES  —————————#
$BkfFolder=”c:\windows\system32\dns\backup”

#—- DELETE DATA IF PREVIOUS BACKUP HAS BEEN PERFORMED  ———#

if ( -not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {

Remove-Item $BkfFolder”\*” -recurse
}

#———————————————–#
# CREATE  FILE WHERE TO STORE DNS SETTINGS
# INFORMATION & WRITE HEADERS
#———————————————–#

$StrFile=$Bkffolder+”\input.csv”
Add-content $StrFile “Zone,ZoneType,Update,Secondary,DsIntegrated”

#—-  GET DNS SETTINGS USING WMI OBJECT ——–#

$List = gwmi -ComputerName $DNSSERVER -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

$List | foreach {

$line=$_.name+”,”+$_.ZoneType+”,”+$_.AllowUpdate

+”,”+$_.MasterServers+”,”+$_.DsIntegrated
Add-content $Strfile $Line

$ZoneName=$_.name
$StrPath=”\backup\”+$_.name

# Export DNS Zone using dnscmd command line

#(available by default in Windows 2008 R2 with DNS console installed)

dnscmd $DNSSERVER “/ZoneExport” $ZoneName $StrPath

}

# End of Script
#——————————————————————————————-#

The last part of the script is where the action happens.  In this part, we simply use WMI object to collect information about the DNS Zones available on the server and additional information such as ZoneType (Active Directory integrated or Secondary…), Secure or not updates and Master IP addresses for secondary,Stub and Conditional Forwarders.

Then we simply call the dnscmd command and we perform an export of the datat into text files.   That’s it. The result will be a bunch of file that will be stored in the c:\windows\system32\dns\backup folder + the input.csv file.  This file will be used as input file when restoring the DNS zones…

Final Notes

Now, probably wondering how to perform a restore of these zones….. Using a similar approach that combines powershell and dnscmd utility.  If you find this script useful, let me know and I might decide to publish the restore DNS Zone script as well (I’m kidding-It will come but do not know when….)

 

Till next time

See ya

 

 

2 thoughts on “Powershell Script :How to Backup DNS Zones

  1. You have some good ideas here but I think you can take this further and make it more Powershell-like. You should be taking advantage of PowerShell cmdlets. For example, instead of manually constructing the CSV file, let PowerShell do it. For the sake of simplicity I’ve separated creating the input.csv from the zone export since I think these can be done separately.

    Create the csv file like this:

    $list | Select Name,ZoneType,AllowUpdate,MasterServers,DsIntegrated | Export-csv input.csv -NoTypeInformation

    Or if you really need the column names the way you specified, then use a hash table:

    $list | Select @{Name=”Zone”;Expression={$_.name}},ZoneType,
    @{Name=”Update”;Expression={$_.AllowUpdate}},
    @{Name=”Secondary”;Expression={$_.MasterServers}},DsIntegrated |
    Export-csv input.csv -NoTypeInformation

    This will create the same CSV file but in a way that leverages PowerShell. Think objects, not parsing text like we did in VBScript. Then to create the zone exports run a command like this:

    $list | foreach {
    $path=Join-Path “backup” $_.name
    $cmd=”dnscmd {0} /ZoneExport {1} {2}” -f $DNSSERVER,$_.Name,$Path
    Invoke-Expression $cmd
    }
    Again, I’m using PowerShell techniques and cmdlets. Building the command with the -f operator is much easier than trying to concatenate.

    One last comment: a recommended scripting best practice in PowerShell is to use full cmdlet and parameter names. It makes it easier to understand without having to decrypt arcane commands like gc.

    Keep at it!

  2. Hi Jeffrey,

    Thanks for the comments and additional explanations….
    You are right, I should start thinking more in powershell terms and starting to let go the good old fellow vbscript.
    Using your approach, I can indeed have a more efficient and powershell based script.
    I’ll try to update this post and take into account your comments.
    Thank you for the input…Really great comments

    See ya

Leave a Reply