Summary: Use Windows PowerShell to work with IP addresses and DNS.

Hey, Scripting Guy! Question Hey, Scripting Guy!

How can I add the DNS settings to the network adapter in Windows Server 2012 R2? I’m a little lost on this one.

—RJ

Hey, Scripting Guy! Answer Hello RJ,

Honorary Scripting Guy, Sean Kearney, is here today to pour out some more PowerShell love. This is the fifth part in a series called Windows PowerShell Network Week. You also might enjoy reading:

I keep thinking of that line from Star Wars, “…and now the circle is complete.” Well, at least mostly for setting up a basic network configuration. We have learned how to identify and work with our network adapters, setup IP addresses, and control the firewall ports. We’ve even seen how we can easily disable power management on the adapters or give them a new name.

But without DNS resolution, we might find that trying to hit our local WSUS server or pulling in our antivirus updates is a bit troublesome. That is unless you want to manually edit all of your host files in your environm…

*THWAP!*

Hey, who threw that?

OK. Editing the host files across an entire enterprise would be a bad idea. That is why we have our DNS servers to keep the name resolution centralized.

Fortunately, this is also easy in Windows PowerShell. There are a series of DNS Client cmdlets to perform all of this magic for us.

The new friend we’re going to be dealing with is the Get-DnsClientServerAddress cmdlet. We can specify a network adapter in its settings to obtain the IP addresses that are in use for DNS.

Get-DnsClientServerAddress –InterfaceAlias “Local Area Connection”

Or we can make our lives easier and ask for only the live network adapters:

Get-NetAdapter –physical | where { $_.Status –eq ‘Up’ } | Get-DnsClientServerAddress

Image of command output

By using the Set-DnsClientServerAddress cmdlet, we can assign DNS configuration to cards that have a static IP address like this:

Set-DnsClientServerAddress –InterfaceAlias “Local Area Connection” –ServerAddresses (“192.168.1.5”)

You can even assign a DNS extension to the network adapter. This is done by using the Set-DnsClient cmdlet:

Set-DnsClient –InterfaceAlias “Local Area Connection” –ConnectionSpecificSuffix “Fabrikam.com”

If you like, you can even pipe a list of adapters with Get-Adapter and assign it “en-masse:”

Get-NetAdapter –physical | Set-DnsClientServerAddress –ServerAddresses (“192.168.1.5”)

But the DNSClient module has powers built in to make your life easier outside of mere network adapter configuration. It has what I think is the coolest cmdlet to date! The Resolve-DNSName cmdlet.

It seems such a simple idea, but this isn’t the same as what Test-Connection does, which is (at most) a name to IP resolution. No, this is the entire power of the old NSlookup console command in one cmdlet!

Something that I used to want to know in my environment was, “Who has the following IP address?” If I was lucky, I could flip the IP to the name with an NBTSTAT command like this:

NBTSTAT –a

That was one of the older methods to obtain the name of a computer given its IP address. But unless you entered into Nslookup, I don’t remember any easy way to do that with a DNS server from the client side. But now there is:

Resolve-DNSName 192.168.1.145

Image of command output

I saw that, and my eyes lit up! “That’s COOL!”

Let’s try some basic stuff that you can now access directly from Windows PowerShell for DNS…show me the MX servers for a particular domain!

Resolve-DNSName –type MX PowerShell.ca

Image of command output

All of this kind of Power is built in to the Windows Server 2012 R2 console. Ready for you.

Hey look, RJ! The weekend is coming up, and believe it or not, we’ve got some more Windows PowerShell coolness coming around the corner tomorrow with WebDAV. Be sure to check it out!

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 remember eat your cmdlets each and every day with a taste dash of creativity.

Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy