Get DNS records

Ask your 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.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Get DNS records

Post by apowershelluser »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

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

Get-WmiObject Win32_NetworkAdapterConfiguration | select ServiceName, DNSDomainSuffixSearchOrder
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

Also this gives you the DNS as "Addresses":

Get-DnsClientServerAddress | select elementname,serveraddresses

"elementName" is the name of the adapter.
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Get DNS records

Post by apowershelluser »

jvierra wrote: Tue Mar 05, 2019 11:01 am 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 3351 times
I can just use this?

(Get-WmiObject Win32_NetworkAdapterConfiguration).DNSDomainSuffixSearchOrder | Sort-Object { $_.length } -Descending | Select-Object -Unique
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

To get only one adapter you need to do this:

Get-WmiObject Win32_NetworkAdapterConfiguration -FIlter 'ServiceName="Netwtw04"'|
select ServiceName, DNSDomainSuffixSearchOrder
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Get DNS records

Post by apowershelluser »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

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. 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. $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
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Get DNS records

Post by apowershelluser »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get DNS records

Post by jvierra »

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.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked