Powershell Script – How to Restore DNS Zones

Hello World,

In a previous post, we have seen how you could backup the dns zones to text files using a mix of powershell and dnscmd.exe. Now, it’s maybe time to check how you can use this backup files in order to restore your dns infrastructure in case of a problem (you have deleted a dns zone).

The principle of this script is similar to the one used to backup DNS Zones. We will again use a mix of dnscmd.exe and powershell scripting. Let’s start the process.

The Restore DNS Script

This is the script.

#———————————————————————-#
# Script_Name : RestoreDNS.ps1
# Description : Perform Restore of DNS Zones of a Windows 2008 DNS SErver
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4
# Date : October 2011
# Created by Griffon
#———————————————————————–#

#– DEFINE VARIABLE——#
#———————————#

# Get Name of the server with env variable

$DNSSERVER=gc env:computername

#—DEFINE WHERE TO Look for DNS BACKUP FILES —–#

$BkfFolder=”c:\windows\system32\dns\backup”

#—Define file name where Dns Settings are Stored

$StrFile=Join-Path $BkfFolder “input.csv”

#—-  RESTORE ZONES BASED ON SETTINGS FOUND IN INPUT.CSV —— #

$Zone=import-csv $StrFile
$Zone | foreach {

$path=”backup\”+$_.name
$Zone=$_.name
$IP=$_.MasterServers
$Update=$_.AllowUpdate
     

#—– Checking if AD Integrated or Not ——-#

if ($_.DsIntegrated -eq $True) {
Switch ($_.ZoneType)
{
1 {
#—– Need to Create Zone As Primary to get all records imported ——-#
$cmd0=”dnscmd {0} /ZoneAdd {1} /primary /file {2} /load” -f $DNSSERVER,$Zone,$path
Invoke-Expression $cmd0
$cmd1=”dnscmd {0} /ZoneResetType {1} /dsprimary” -f $DNSSERVER,$Zone

}

3 { $cmd1=”dnscmd {0} /ZoneAdd {1} /dsstub {2} /load” -f $DNSSERVER,$Zone,$IP }
4 { $cmd1=”dnscmd {0} /ZoneAdd {1} /dsforwarder {2} /load” -f $DNSSERVER,$Zone,$IP }
}
} else {

Switch ($_.ZoneType)
{
1 {$cmd1=”dnscmd {0} /ZoneAdd {1} /primary /file {2} /load” -f $DNSSERVER,$Zone,$path}
2 {$cmd1=”dnscmd {0} /ZoneAdd {1} /secondary {2}” -f $DNSSERVER,$Zone,$IP }
3 {$cmd1=”dnscmd {0} /ZoneAdd {1} /stub {2}” -f $DNSSERVER,$Zone,$IP }
4 {$cmd1=”dnscmd {0} /ZoneAdd {1} /forwarder {2}” -f $DNSSERVER,$Zone,$IP }
}
}

 #Restore DNS Zones  
Invoke-Expression $cmd1

Switch ($_.AllowUpdate)
{
#No Update
0 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}
#Secure and non secure
1 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}
#Only Secure Updates
2 {$cmd2=”dnscmd /Config {0} /allowupdate {1}” -f $Zone,$Update}

}

#Reset DNS Update Settings
Invoke-Expression $cmd2

}

# End of Script
#———————————————————————–#   

 

A bit of explanation

The first part of the script is used to define some variables.  We simply define where the script has to look in order to retrieve the backup files and the “special” text file called input.csv. This file contains valuable information about the dns zones you are about to restore. It will tell the script if you need to restore a AD integrated zone or a secondary zone….

In the middle part, we simply use the import-csv cmdlet to gather the information contained in the input.csv file

The final part of the script will parse the input.csv file and based on the type of zone (AD Integrated or not), the script routine will start the dnscmd command line utility using different parameters based on the zone format you need to restore (primary,secondary,stub or forwarder zones)

Final Comments

The script should be quite straightforward and self-documented.  Note that this script will work to “recreate” a DNS zone.  If the zone you are trying to restore is still present on the DNS Server, the dnscmd.exe utility will return a warning information telling you that the zone already exists.  You might need to delete the zones before restoring them.

Again, this script is provided to you as a starting point.  Obviously, you could add an operation in the script telling that you might want to delete the zone before restore it. That’s up to you

Finally, please free to comment and improve this script.

Till next time see ya

4 thoughts on “Powershell Script – How to Restore DNS Zones

  1. Great! My suggestion here to make it more “PowerShell” like is to look at the Switch statement. I think you can use Switch instead of the If/elseIf. When you have as many elseif statements, I think Switch is a little easier to read. I’ll get you started:

    Switch ($zonetype) {

    }

  2. Hello Jeffrey,
    I know -not an excuse – but It was a little bit late when I published the post. I preferred to use the if else because that was no brainer. If I have some time left, I might update the script and insert the switch statement

    Thank you for the comments – See ya soon – I’m preparing another script (and I have a small issue with it) 🙂
    till next time
    see ya

  3. Getting error.. 🙁
    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:76 char:19
    + Invoke-Expression $cmd2
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Invoke-Expression : Cannot bind argument to parameter ‘Command’ because it is null.
    At C:\IT Support\restore_dns.ps1:62 char:19
    + Invoke-Expression $cmd1
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

  4. @Kumar,

    just a guess here… do you have the dnscmd.exe command available on the machine where you running the script ?
    Are you running the script directly on the DNS server or you are executing from a different server ?
    Can you open a command prompt and simply type dnscmd.exe and see if it display an error or the actual help ?

    So far not sure what could be the issue…. looking at the error message , $cmd1 or $cmd2 seems empty…this is why I’m asking to check if the dnscmd.exe is available on the machine where the script is running
    waiting for your feedback
    Till next time
    See ya

Leave a Reply