Thursday, November 12, 2015

Powershell add A resource records in DNS Domain oneliner


This is a small article on how add A resource record in DNS server. Also in the last I am going to show how to add Resource record inside domain using powershell. Here I will be demostrating below 3 cmdlets. 
Add-DnsServerResourceRecordA
Add-DnsServerResourceRecord
Get-DnsServerResourceRecord

My DNS server is running on windows server 2012 R2. In this tutorial I have downloaded and  installed RSAT remote server administration tools on my Windows 8.1 desktop, so I can configure dnsserver remotely, once RSAT is installed go to Control Panel >> Programs >> Turn windows Features On and off  >> Remote Server administration tools,  install DNS administration tools,  now I can manage my DNS server remotely, specifically I have installed DNSSERVER powershell module. (Whenever first time I run any DNS related command to DNSServer module it loads it automatically, I dont need to import it explicitly on windows 2012 and 8)

Checkout my another Article: Powershell find missing associated PTR resource records in DNS Server
 
As above screenshot I have added DNS record entry for TestMachine01 - 192.168.33.150. Open  Powershell, and fire up below command 
Add-DnsServerResourceRecordA -Name TestMachine01 -IPv4Address 192.168.33.150 -ZoneName vcloud.lab -ComputerName Ad001 -CreatePtr

Note: To view the changes in DNS manager you will need to right click and refresh the zone, if you have already opened DNS manager.

I have break down command in as below .
Add-DnsserverResourceRecordA = This is the CMDLET used to add A resource record only.
-Name TestMachine01 = -Name is parameter and TestMachine01 is computer name of A resource record name.
-IPv4Address 192.168.33.150 = This self explanatory.
-ZoneName vCloud.lab = vCloud.lab is my zonename.
-ComputerName AD001 = -ComputerName is parameter for Dnsserver, AD001 is my DNSserver.
-CreatePtr = This is optional, if you want to create ptr (Reverse lookup record entry).

This part is to add only single DNS host entry, what if you want to add multiple resource record from excel file, Here is below step by step tutorial. I have saved excel file as csv. (and it is saved in c:\temp location).

When I open CSV in notepad, data looks like this
Now in the powershell fire below command 
Import-Csv C:\Temp\DnsEntries.csv | ForEach-Object { Add-DnsServerResourceRecordA -Name $_.Name -IPv4Address $_.IPv4Address -ZoneName vcloud.lab -ComputerName Ad001 -CreatePtr}

This will import csv file and it will piped into foreach-object loop, then foreach row record is added (value of $_ is always get changed row changes)
Now we have added these records how do I verify it. use Get-DnsServerResourceRecord command
For single computer:
Get-DnsServerResourceRecord -Name TestMachine01 -ZoneName vCloud.lab -ComputerName Ad001 

For mulitple computer we will use the same CSV file format. (IPv4Address column is not required)
Import-csv C:\temp\DnsEntries.csv | ForEach-Object {Get-DnsServerResourceRecord -Name $_.Name -ZoneName vCloud.lab -ComputerName Ad001}

This tutorial ends here, and now I want to show how to add resource record entry in Domain created under DNS Zone. I have one Dns zone name local and under it there are mulitple Domains. and I will be using Mylab domain as a demo.
Earlier I used command Add-DnsServerResourceRecordA as a demo in this I am showing another Powershell command Add-DnsServerResourceRecord. differance between both is there A missing in the last of Add-DnsServerResourceRecordand only capable of creating any record, in contrary Add-DnsServerResourceRecordA shown earlier can only create A resource record. Command can create all type of record but the resource type need to be mentioned, as i done below -A

Add-DnsServerResourceRecord -Name Lab002.MyLab -IPv4Address 192.168.32.152 -ZoneName Local -ComputerName Ad001 -A
 
To breakdown above command I have suffixed .Mylab domainName in the -Name and additional parameter is -A to declare it is host A record.

For adding multiple host A record entries use below one-liner.
Import-Csv C:\temp\DnsEntries.csv | ForEach-Object {Add-DnsServerResourceRecord -Name $_.Name -IPv4Address $_.IPv4Address -ZoneName Local -ComputerName Ad001 -A}

And in the last to collect and verify added records by using below oneliners
For single computer
Get-DnsServerResourceRecord -Name Lab002.MyLab -ZoneName Local -ComputerName Ad001

For multiple computer
Import-Csv C:\temp\DnsEntries.csv | ForEach-Object {Get-DnsServerResourceRecord -Name $_.Name -ZoneName Local -ComputerName Ad001}
Post a Comment