Get DNS records

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Get DNS records

Post by JohnTitor » Tue Mar 05, 2019 11:56 am

I've looked on the web and I know there are native DNS cmdlets to get the DNS names. However these don't work in my environment.

So I have this code

Get-WmiObject -Query 'SELECT DNSDomainSuffixSearchOrder FROM Win32_NetworkAdapterConfiguration' | select -ExpandProperty DNSDomainSuffixSearchOrder

Is it just me, or does that list duplicate the names?

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 12:01 pm

Just you. Try it this way to understand what is happening:

Get-WmiObject Win32_NetworkAdapterConfiguration | select ServiceName, DNSDomainSuffixSearchOrder

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 12:08 pm

Also this gives you the DNS as "Addresses":

Get-DnsClientServerAddress | select elementname,serveraddresses

"elementName" is the name of the adapter.

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Get DNS records

Post by JohnTitor » Tue Mar 05, 2019 12:15 pm

jvierra wrote:
Tue Mar 05, 2019 12:01 pm
Just you. Try it this way to understand what is happening:

Get-WmiObject Win32_NetworkAdapterConfiguration | select ServiceName, DNSDomainSuffixSearchOrder
Get-WmiObject Win32_NetworkAdapterConfiguration | select ServiceName, DNSDomainSuffixSearchOrder | select -ExpandProperty DNSDomainSuffixSearchOrder
Yeah, this duplicates the DNS for some weird reason
This is the command I'm looking for as the first one doesn't pull the info I need.
DNS.png
DNS.png (73.3 KiB) Viewed 1318 times
I can just use this?

(Get-WmiObject Win32_NetworkAdapterConfiguration).DNSDomainSuffixSearchOrder | Sort-Object { $_.length } -Descending | Select-Object -Unique

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 12:43 pm

Unfortunately what you think you are doing will not work.

You need to pick exactly one adapter and your issues will resolve. YOU changed my code and it just took you back to the wrong outcome.

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 12:47 pm

To get only one adapter you need to do this:

Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'ServiceName="Netwtw04"'|
select ServiceName, DNSDomainSuffixSearchOrder

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Get DNS records

Post by JohnTitor » Tue Mar 05, 2019 12:56 pm

No.. YOU assume that I don't know what I'm doing. Unfortunately, you jumped the gun and provided a solution to your assumption.

Go back to my op, and run the script I gave, what are the results? Just a list of the DNS's right. I asked if it's just me or is it like that for everyone.

Get-WmiObject -Query 'SELECT DNSDomainSuffixSearchOrder FROM Win32_NetworkAdapterConfiguration' | select -ExpandProperty DNSDomainSuffixSearchOrder

Since that's the case for me.. My solution is this

[System.Collections.ArrayList]$DNSList = (Get-WmiObject Win32_NetworkAdapterConfiguration).DNSDomainSuffixSearchOrder | Sort-Object { $_.length } -Descending | Select-Object -Unique

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 2:02 pm

Ok. I will try to explain what is happening.

The normal method or at least the most common method of doing this is like this:

Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'IPEnabled=True'| select ServiceName, DNSDomainSuffixSearchOrder

Which will return the following:
  1. ServiceName DNSDomainSuffixSearchOrder
  2.  
  3. ----------- --------------------------
  4.  
  5. Netwtw04    {hsd23.ny.comcast.net}
Notice the {} surrounding the DNS. It is an array. When you expand multiple arrays by using the "-expand" switch the arrays are merged in the pipeline. If you pick one adapter then you won't get duplicates. We can also do the following:
  1. [array]$adpt = Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'IPEnabled=True'
  2.  
  3. $adpt.DNSDomainSuffixSearchOrder
This is pretty standard PowerShell behavior with arrays and it can cause confusion. Simple arrays are always merged in the pipeline.

This is another way to look at the behavior:

(Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'IPEnabled=True').DNSDomainSuffixSearchOrder

Which is essentially the same as using "-expand".

User avatar
JohnTitor
Posts: 153
Joined: Fri Nov 17, 2017 8:49 pm

Re: Get DNS records

Post by JohnTitor » Tue Mar 05, 2019 4:03 pm

I appreciate the breakdown, and I get all of it. Funny thing, if I run

(Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'IPEnabled=True').DNSDomainSuffixSearchOrder

it returns duplicates. Which if I'm reading you right, it should not :D ()
This is why I need to use the -unique

If I use
Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'ServiceName="Netwtw04"'|
select ServiceName, DNSDomainSuffixSearchOrder

It returns nothing but the service name.

Why I'm doing this:

I ping a computer and lets say its in our California office, and on wireless. It will have a different DNS than our New York offices on wireless.

So, I ping the computer to test it's online, then I ping the computer foreach DNS to get my fully qualified domain name
some of the computers on our domain require the FQDN to access

another reason I do this is so I can turn my results into an object that I can display in a datagridview if need be


If, there is an easier way to which DNS a remote computer is connected to, and can output it to an object, I'm all ears

:D

jvierra
Posts: 13893
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get DNS records

Post by jvierra » Tue Mar 05, 2019 4:15 pm

No. The point is that any time you expand the property in any way then, because it is an array, the values will be combined. Selecting only one returned object is the only way to avoid duplicates.

Locked