Latest posts by Adam Bertram (see all)
- How to add leading and ending zeroes to strings in PowerShell - Thu, Dec 7 2017
- How to publish scripts to the PowerShell Gallery - Tue, Dec 5 2017
- Strings in PowerShell: Quotes, formatting, and concatenation - Mon, Dec 4 2017
PowerShell allows you to not only manage your DNS records from the command line but also to take those commands and put them into a script to automate all kinds of time-consuming tasks.
In this article, I will show you how to make that initial connection to your DNS servers from PowerShell and then go over a few common examples of tasks that you might find yourself needing to accomplish in the future.
Before we get too far, you need to be aware of a few prerequisites. First, I’m assuming you have permissions to read, modify, and remove DNS records from your Windows servers.
Second, I’ll be demonstrating a few concepts from DNS servers that are in an Active Directory domain with AD-integrated zones. PowerShell is still capable of managing zones and records outside of Active Directory but may not offer quite the same result as I’ll be showing you here.
Finally, you’ll need to ensure you have a version of the Remote Server Administration Tools (RSAT) installed on your client specific to your operating system.
Now that we have that out of the way, let’s start out by first ensuring the DNSServer module is available to us. To do that, I’ll use the Get-Module cmdlet.
Get-Module DNSServer –ListAvailable
Verifying that the DNSServer module is available
Great! It looks like it is. Next, whenever exploring new functionality, it’s always a good idea to start with a Get PowerShell cmdlet to simply read an object. This ensures you can make the connection to the server and have permission to at least read objects.
Because we’re managing DNS records in this article, I’ll first start off by using Get-DNSServerResourceRecord. This cmdlet allows us to pull DNS records from one or many different DNS zones on a Windows DNS server.
I’m testing this in an Active Directory domain called mylab.local so, naturally, a mylab.local DNS zone has already been created on my domain controller. I’ll use the cmdlet to query that DNS zone on the domain controller called DC.
Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local
Reading DNS records with PowerShell
No red text—good! It looks like I can successfully pull all of the DNS records from a particular zone. What if I need just a particular type of record rather than all records? It’s not usual for me to fiddle with RV and NS records. I spend most of my time managing the A records. No problem: simply use the RRType parameter and specify an A. You’ll see that it only returns A records.
Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -RRType A
Now that I can read various DNS records, perhaps I’d like to modify a static record. One of our server names has changed and I need to be sure its DNS record is updated to reflect that. Changing DNS records is a little convoluted but, with some tenacity, we can still make it happen.
First, we’ll need to get two identical objects representing a DNS record. In this case, I’m pulling a DNS record for my MySQL server.
$new = $old = Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL
After I have the two objects, I’ll then change the IPV4 address on the new object to represent the IP address it has changed to. Unfortunately, it’s not quite as easy as simply setting a string. The IPV4Address property requires a type of System.Net.IPAddress in order to successfully make the change.
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse('192.168.0.254')
After the IP address is changed on the $new object, I can then use Set-DNSServerResourceRecord to force PowerShell to update the record on the server itself.
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName mylab.local -ComputerName dc
Finally, if I’d like to remove the record, the process is much simpler. I can simply pipe the results of Get-DNSServerResourceRecord directly to Remove-DNSServerResourceRecord.
Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL | Remove-DNSServerResourceRecord –ZoneName mylab.local –ComputerName DC
You can do so much more with DNS records with PowerShell. To get a full list of all of the various commands in the DNSServer module, use the Get-Command cmdlet.
Get-Command -Module DNSServer -Name *record*
Also, always remember to use Get-Help if you’re curious about what a particular cmdlet might do! Get-Help is a great way to explore new cmdlets and functionality in PowerShell.