DNS information

I want to get DNS information from hosts in a particular OU. I need the results to be put into excel. The only information I need is what ip address they are using for DNS. Once that is reported I want to set the DNS on the hosts from the particular OU.

I have been reading and thought I was making some progress with

PS C:\> Get-WmiObject Win32_NetworkAdapterConfiguration -computername xxxxxx | select name
, DNSServerSearchOrder
name                                          DNSServerSearchOrder
----                                          --------------------

                                              {192.168.10.2}

However as you see the name does not return and this is only for a particular computer. I also cant seem to get anywhere with code that would get it to go to AD and a particular OU as well as export to excel…Any help or pointers would be appreciated. Again I am new at this so if it is something obvious that I am missing it is because I’m not always sure what exactly I am supposed to be looking for.

If you pipe the results to Get-Member you can see what methods and properties an object has.

Get-WmiObject Win32_NetworkAdapterConfiguration -computername xxxxxx | Get-Member

It doesn’t look like there’s a name property available.

Try:

Get-WmiObject Win32_NetworkAdapterConfiguration -computername xxxxxx | select PSComputerName,DNSServerSearchOrder

Any reason you’re setting it statically and not via DHCP?

To get the computers in a particular OU use the Get-ADComputer cmdlet and specify the -Searchbase parameter.

Get-ADComputer -Filter * -Searchbase "ou=computers,ou=marketing,ou=wales,dc=contoso,dc=com"

To export to Excel, use Export-CSV

You can get detailed help for all cmdlets with Get-Help .

Thanks for your reply.

I just started with this particular company and everything here is assigned static. They had some things in place long ago that required this and it was never changed due to a “not broke, don’t fix” type of thing. I am in the process of playing catch up to get moving forward towards current standards and norms. This is a big task for me as I have not had this type of control previously.

I’m trying to move forward with the help you have provided and will update with what I come up with. My end goal is to have a nice spreadsheet with two columns. Computername or IPaddress or whatever I can get and the ip of its current dns server.

Ok I’m really making progress now (at least for my level of understanding). I now have:

PS C:\> get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxx,DC=cityofxxxxxxx,DC=com" | expo
rt-csv c:\reports\exporttest.csv

and the

Get-WmiObject Win32_NetworkAdapterConfiguration -computername xxxxxx | select PSComputerName,DNSServerSearchOrder

works.
So now I am beating my head against the wall reading the help to figure out how to put these together. I feel like I’m missing something because I’m so close. I have everything I need now but I cant put it together.

This is what I have now:

PS C:\> get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxx,DC=cityofxxxxx,DC=com" | sele
ct -expandproperty name | foreach {get-wmiobject win32_networkadapterconfiguration} | select pscomputername,dnsser
versearchorder | export-csv c:\share\exporttest.csv

When I run this I don’t get an error but it does not return the DNSserversearchorder value in the spreadsheet. It is only returning the PScomputername with DNSserversearchorder blank? Any ideas? I’m so close! Or maybe not so much.

for DNSServerSearchOrder I am getting System.String instead of the IP address of the dns server. I have tried a couple things I have been reading about like joining but then I get “dnsserversearchorder” already exists.

update I am now getting a return in the dnserversearchorder column but not for every device. Something is going on because it only gives me the dns address every 11 entries exactly so everything in between each 11 entries is blank. I am currently using:

PS C:\> get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxx,DC=cityofxxxxx,DC=com" | sele
ct -expandproperty name | foreach {get-wmiobject win32_networkadapterconfiguration} | select pscomputername,@{"nam
e"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} | export-csv c:\share\exporttest.csv

Any Ideas out there?

Well done for getting this far. I suspect that your computers have multiple adapters (not necessarily physical ones) and currently your query returns all of them. Your best bet will be to create a filter so that the Get-WmiObject cmdlet only returns the adapter you’re interested in. As your machines are domain joined, using the DNSDomain property is worth a shot.

foreach {get-wmiobject win32_networkadapterconfiguration -filter "DNSDomain='contoso.com'"}

It runs with no errors but the exporttest.csv is now totally blank. No columns or anything. I do know that the adapter will be local area connection. Can that be used?

Ok so now I’m using

PS C:\> get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxx,DC=cityofxxxxx,DC=com" | sele
ct -expandproperty name | foreach {get-wmiobject win32_networkadapterconfiguration -filter "ipenabled='true'"} | s
elect pscomputername,@{"name"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} | export-cs
v c:\share\exporttest.csv

and the report is better but now instead of returning dns ip every 11 it is returning every 4th pscomputername entry.

I’m kinda stumped at this point. I’m getting closer but I do not understand the theory and logic as to why is would skip to every 4th since that is not random.

Take a step back and have a look at what the WMI query returns for just one computer. For example, if I run

get-wmiobject win32_networkadapterconfiguration -filter "ipenabled='true'"

on my machine, it returns three adapters. Only one has DNS entries. If I run that on two similar computers, it’s going to look like this:


“PSComputerName”,“dnsserversearchorder”
“PC-1”,“”
“PC-1”,“”
“PC-1”,“8.8.8.8;192.168.7.1”
“PC-2”,“”
“PC-2”,“”
“PC-2”,“8.8.8.8;192.168.7.1”

Clearly your filtering is working, you’ve reduced the number of adapters it’s returning. You just need to use Get-Member and figure out exactly which property to use to return the single adapter (or cheat and hide the blank lines in Excel :slight_smile: )

Yeah you are right it is working. And I will go the hiding blank lines route since the report is for me only anyway.

One thing I just noticed is that now my PScomputername is just returning a my hostname and now a list from AD…if its not one thing it is another lol. If you have any ideas on that let me know.

Thanks again for your help on this. I am trying to learn in a hurry.

sorry I was typing as walking out the door.

UPDATE:

It is still not working properly when the commands are all together.

get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxx,DC=cityofxxxxxx,DC=com" | s
elect dnshostname | export-csv c:\share\exportest.csv

works and returns a all of the dns hostnames but when combined.

get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxxx,DC=cityofxxxxxx,DC=com" | s
elect dnshostname | foreach {get-wmiobject win32_networkadapterconfiguration -filter "ipenabled='true'"} | select pscomput
ername,@{"name"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} | export-csv c:\share\exporttest.
csv

All the hostnames are replaced with my hostname for all of the entries. I have tried multiple variations but whenever the two are combined the dnshostname field doesn’t work right.

Do not use select statements, keep objects as objects. When you are writing a script get in the habit of working with one until the output is appropriate.

$computers = get-adcomputer somecomputer

#get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxxx,DC=cityofxxxxxx,DC=com" 


foreach($computer in $computers){

$nics = get-wmiobject win32_networkadapterconfiguration -filter "ipenabled='true'" -computername $computer.dnshostname

foreach($nic in $nics){

[pscustomobject]@{

computername = $computer.name
searchorder = $nic.dnsserversearchorder -join '; '



}

}


} 




Alex, your problem is with your ForEach. You are not telling get-wmiobject to use the current input, so it’s defaulting to your system each time through the loop. Additionally you should expand the property so that it is passing each individual name to the pipeline rather than an object with a dnshostname property.

Try:

get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxxx,DC=cityofxxxxxx,DC=com" | s
elect -expandproperty dnshostname | foreach {get-wmiobject win32_networkadapterconfiguration -computername $_ -filter "ipenabled='true'"} | select pscomput
ername,@{"name"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} | export-csv c:\share\exporttest.
csv

To Dan’s point, you don’t need to use select after your get-adcomputer. Select is good for getting just the properties you want when working in the pipeline after you have the objects you want that data from. So usually toward the end if the pipeline, just before the output.

get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxxx,DC=cityofxxxxxx,DC=com" |
foreach-object {
    get-wmiobject win32_networkadapterconfiguration -computername $_.dnshostname -filter "ipenabled='true'"
} |
select-object pscomputername,@{"name"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} |
export-csv c:\share\exporttest.csv

Awesome! Thanks for that. It is returning the unique hostnames and the dnsserversearchorder now. I am embarrassed to say that I don’t understand the format that you are using. I literally am just a couple weeks into powershell and I am learning on the job.

What you provided works when I save it as a script and that is awesome but I really need to understand how that formatting works. so of course my question now is why cant I make it export to csv

Understand objects, properties and methods and you’ll be well on your way to understanding PS:D

When using the select prior to the pipeline we’re effectively transforming the object into a pscustomobject that has no properties.

PS H:> (get-adcomputer mycomputer | select name).gettype()

IsPublic IsSerial Name BaseType


True False PSCustomObject System.Object

PS H:> (get-adcomputer mycomputer).gettype()

IsPublic IsSerial Name BaseType


True False ADComputer Microsoft.ActiveDirectory.Management.ADAccount

While the select would work in your scenario, I feel it’s important to stay consistent. This will help when you start piping into set-adcomputer,set-aduser etc. Since we’re not expecting output of object properties we just pass the full object through the pipeline and reference the name or dnshostname property for the get-wmiobject -computername parameter.

Use select for output at the end only if necessary. Expressions are useful but I think the pscustomobject hashtable is very quick and the format is easily readable for beginners.

The double foreach is necessary to account for multiple nics.

You’ll probably want to throw a test-connection in there or setting the variable to null to avoid populating the dnsserversearchorder for computers that are not online.

WINNER WINNER! Dan and Curtis get the Chicken dinner! Thanks so much for help. Thank everyone on this thread for your help. It is working like a charm and returning the data nicely in the export. I really do appreciate it.

I apologize for my rookie mistakes here but I think this thread will be a good read for someone new like me.

Just to close this nicely the final is:

get-adcomputer -filter * -searchbase "OU=__Computers,OU=_xxxxxx,DC=cityofxxxxxx,DC=com" |
foreach-object {get-wmiobject win32_networkadapterconfiguration -computername $_.dnshostname -filter "ipenabled='true'"} | select-object pscomputername,@{"name"="dnsserversearchorder";"expression"={$_.dnsserversearchorder -join ";"}} | export-csv c:\share\exporttest.csv

Thanks again for all the help. By far the most helpful people on any forum ive been on.