DNS entry addition via Powershell

http://blog.nessus.nl/410/adding-dns-records-using-powershell/

I found this on Marcel's Blog and quite like the simplicity of the process. I may or may not add this to the one-liners post but I thought this useful to warrant it's own post.

Paraphrase and clarification of his post:

He posted two scripts that show addition of IP addresses into the DNS server using a simple script.

1..32 | % {iex ([string]::format(“dnscmd /RecordAdd mylab.com ESXhost{0} /createPTR A 192.168.10.{0}”,$_))}

But doesn't explain what this script is doing and upon first glance is confusing to say the least. He is populating DNS with consecutive IP addresses and consecutive hostnames.

ESXhost1 192.168.10.1
ESXhost2 192.168.10.2
...

Ingenious script once you understand what it is doing, but impractical unless you have this type of naming and numbering setup.

Explanation:
1..32 creates an array from 1 - 32
iex = invoke-expression
[string]::format - input the string 1 - 32 into the following expression (" ")
             dnscmd (command line executable only available on DNS servers)
                           RecordAdd Zone <Hostname> /CreatePTR A <IPAddress>
This creates a record in the DNS and a PTR record in the reverse DNS value.

The next script creates the entry from the values in a CSV file. Firstly, create the CSV file with the following format (Including headers):

Hostname, IPaddress
ESXHost01,192.168.10.20
ESXHost20,192.168.10.50
VCServer01,10.10.1.34
...

Then run the one liner script as seen below:

Import-CSV <path to csv file> | foreach {dnscmd /RecordAdd <domName> $_.hostname /createPTR A $_.IPaddress}

If you have multiple zones then replace the <domName> with a $_.Zone and add the Zone entries in the CSV file. I have had DNScmd return an error but the DNS entry is created.

Explanation:
Import-csv is exactly what it specifies import a CSV file in an array.
For Each of the entries perform a DNSCMD using the values in the CSV






Comments