Summary: Manage DNS server settings with Windows PowerShell in Windows Server 2012 R2.
Honorary Scripting Guy, Sean Kearney, is here. Our final part of the Windows PowerShell Network Week series has arrived. You also might enjoy reading:
- Windows Server 2012 R2 Network Cmdlets: Part 1
- Windows Server 2012 R2 Network Cmdlets: Part 2
- Windows Server 2012 R2 Network Cmdlets: Part 3
- Windows Server 2012 R2 Network Cmdlets: Part 4
- Windows Server 2012 R2 Network Cmdlets: Part 5
- Windows Server 2012 R2 Network Cmdlets: Part 6
Today I’m going to show you a couple of really cool and easy ways to work with your DNS server. These are things I never had working in the field that would have made my life easier.
I remember going to sites and wishing I had an easy way to pull the configuration of a DNS server and store it away for later use. Well, now I can!
Built into Windows Server 2012 R2, is a simple report called Get-DnsServer, which will produce the output of my current configuration. Here’s a quick output from my test server at Energized.local:
Get-DNSServer
There’s a lot more to this report, but I could take this away for later viewing with a simple Export-CLIXML cmdlet:
Get-DNSServer | Export-CLIXML DnsConfig.xml
I found that to be pretty cool! On any workstation that has Windows PowerShell, I can review this report by running this cmdlet against the same file —even without the cmdlets or the DNS Server service:
Import-CLIXML Dnsconfig.xml
Do you need to get a list of DNS zones on your server?
Get-DNSServerZone
How about a list of all DNS records for a particular zone? That’s something I could do before—but never as a straight object. Now I can say show me all records of type “A” for a particular zone like this:
Get-DNSServerZone ‘Contoso.local’ | Get-DNSServerResourceRecord –Rrtype A
You can also do more than reporting on a DNS server’s configuration. You can create information within the DNS server. Here, we create a brand new zone called Contoso.local, which replicates to all forests:
Add-DNSServerPrimaryZone –name ‘Contoso.local’ –replicationscope Forest
We now do some pretty cool naming, like creating records in said zone, directly from the Windows PowerShell console:
Add-DnsServerResourceRecord -ZoneName 'contoso.local' -IPv4Address '192.168.1.35' -Name ‘ContosoWeb1' -A
This line would have created an “A” record called “ContosWeb1” in the Contoso.local domain with an IP address of 192.168.1.35. This means creating DNS records en masse is now far easier.
Or better yet, what if you need to edit a lot of records? We can now change the value of DNS records in Windows PowerShell quite easily. Let’s say the value of the record for our web server needed to change to 10.0.0.35 because we moved the server to our DMZ zone. We can execute this from Windows PowerShell like this:
$OldRecord=Get-DNSServerResourceRecord –name ‘ContosoWeb1’ –Zonename ‘Contoso.local’
$NewRecord=$OldRecord
$NewRecord.RecordData.IPV4Address=”10.0.0.35”
Set-DNSServerResourceRecord –oldinputobject $OldRecord –newinputobject $NewRecord –zonename ‘Contoso.local’
Are you new to managing Windows Server? Or do you have an infrastructure running without the GUI? Never fear. With just a little bit of Windows PowerShell magic, you can manage all of your network needs! And that concludes Windows PowerShell Network Week.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send an email to the Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then just remember, the Power of Shell is in You.
Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy
0 comments