Create a bunch of DNS Entries using PowerShell and DNScmd.exe

2012/11/01 | 1 minute read |

Update 2019/06/28: Add example to add the same entries to multiple zones.

Today I needed to create approx. ~50 DNS A entries. Each of those also need to have a PTR entry.

Lazy as i am… a quick search for PowerShell DNS module did return some interesting things but none who can create both A and< PTR DNS entries at the same time.

So I decided to finally use DNSCMD.exe (full syntax on technet) with powershell.

Requirement: DNSCmd.exe is part of theDNS Server Tools and need to be installed prior to use it. On Windows server you can install it using Add-WindowsFeature RSAT-ADDS-Tools

Here is a quick syntax overview of the Dnscmd.exe that we will be using.

dnscmd.exe <DNSServer> /RecordAdd <DNSZone> <NewEntryName> /CreatePTR A <IPAddress>

First Step: Create a CSV with all the information (in Excel or via PowerShell) here is an example, save it as DNSEntries.csv (in my case at the root of the C: drive)

Second Step: The PowerShell one-liner:

Import-CSV -Path "c:\DNSEntries.csv" |
ForEach-Object {
  dnscmd.exe $_.dnsserver /RecordAdd $_.zone $_.name /createPTR $_.type $_.IP
}

And the output should look like the following:

Extra: Adding the same entries to multiple zones.

One user asked in the comments how to add the same DNS entries to multiple zones.

This could be accomplished uing the following approach:

Note: I did not try this

First step

  1. create a csv file, similar to the previous example and remove the zone property. (just remove the column zone and save the file)
  2. create a text file that contains the list of your zones.

Second step

# Assuming you want to create the same entries for
# multiple zone, here is the approach you would do

# Get the DNS entries from dnsentries.csv
$DNSEntries = Import-CSV -Path "c:\DNSEntries.csv"

# Get the zones from the zones.txt file
$MyZones = Import-CSV -Path "c:\zones.txt"

# For each DNS Entries
$DNSEntries | ForEach-Object {
	# Capture current DNS Entry to process
	$CurrentDNSEntry = $_
	
	# Foreach Zones
	$MyZones | ForEach-Object {
		# Capture current DNS Entry to process
		$CurrentZone = $_
		
		# Create "CurrentDNSEntry" in "$CurrentZone"
		"[Zone: $currentzone] Entry name $($CurrentDNSEntry.name)"
		dnscmd.exe $CurrentDNSEntry.dnsserver /RecordAdd $CurrentZone $CurrentDNSEntry.name /createPTR $CurrentDNSEntry.type $CurrentDNSEntry.IP
	}
}

Leave a comment