Managing DNS Records via Powershell

Adam Bertram

Adam Bertram

Read more posts by this author.

Before you get going with managing DNS with PowerShell I’m assuming you’re not going to doing this directly on a server. If you are, shame on you! With PowerShell, you should never have to be on the console!

Want to learn more about managing DNS records with PowerShell? Why not pick up and begin learning about DNS records in this detailed, step-by-step, tutorial on managing DNS records.

To manage DNS on your Windows 7/8 workstation you first have to download the Remote Server Administration Tools (RSAT) and get it installed and enabled via Programs and Features in Control Panel. RSAT is a much for any Microsoft system administrator.

Once you’ve got the proper tools in place you can now begin to check out all of the DNS management capabilities available to you. Type Get-Dns and start hitting tab. I’ll wait until you’re done being mesmerized over everything that you can read via the DnsServer module.

Now that your mind is officially blown lets start by checking out all the zones on a Microsoft DNS server. Once we find all the zones we can then dive deeper and retrieve all the records from each of those zones if we so choose to.

Scenario #1 – Finding PTR records in all zones

We’re using Microsoft’s Dynamic DNS and we’ve got a DHCP server creating crazy PTR records for some reason. We need to investigate what it has done.

For some reason, the DHCP server keeps on creating records with a -BAD name in them. We have to find all of the -BAD record names within all reverse zones and FAST!

We first start with enumerating all of the reverse DNS zones.

## Retrieve all DNS zones from the specified DNS server
Get-DnsServerZone -Server DNS_HOSTNAME | ? {$_.ZoneName -like '*in-addr.arpa'}
Finding DNS zones with Get-DnsZone
Finding DNS zones with Get-DnsZone

Once we get the list of zones we now have to find all of the PTR records inside each zone matching our -BAD name.

Get-DnsServerZone -Server dc01 | ? {$_.zonename -like '*in-addr.arpa'} | Get-DnsServerResourceRecord -ComputerName dc01 -RRType PTR | ? {$_.Hostname -like '*-BAD'}
Finding DNS resource records with Get-DnsServerResourceRecord
Finding DNS resource records with Get-DnsServerResourceRecord

Awesome! We’ve successfully retrieved all of the PTR records matching the hostname of -BAD!

Scenario #2 – Using PowerShell to create static DNS A records for a list of servers

We just got in a shipment of servers and we now need to create static A records in our main zone so that users can access these servers.

The first thing we’ve got to do is import our CSV with server names and IP addresses into our script.

$servers = Import-Csv server_names.txt

After we’ve got the server names and IPs in a variable we can now begin creating the A records via a foreach loop.

$servers | % { Add-DnsServerResourceRecord -ZoneName 'my_main_zone' -A -Name $_.ServerName -IPv4Address $_.IPAddress }

That’s it! We’ve quickly added dozens or hundreds of A records to our DNS zone via a simple CSV file.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!