DNS information

Tagged: 

This topic contains 18 replies, has 4 voices, and was last updated by Profile photo of Alex Duke Alex Duke 1 month, 2 weeks ago.

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #50177
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    #50181
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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 .

    #50185
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    #50196
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    • This reply was modified 1 month, 2 weeks ago by Profile photo of Alex Duke Alex Duke.
    #50222
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    #50224
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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?

    #50230
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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'"}
    #50235
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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?

    #50237
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    #50241
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    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 🙂 )

    #50243
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    #50248
    Profile photo of Alex Duke
    Alex Duke
    Participant

    sorry I was typing as walking out the door.

    #50453
    Profile photo of Alex Duke
    Alex Duke
    Participant

    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.

    • This reply was modified 1 month, 2 weeks ago by Profile photo of Alex Duke Alex Duke.
    #50471
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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 '; '
    
    
    
    }
    
    }
    
    
    } 
    
    
    
    
    
    #50483
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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
    
    • This reply was modified 1 month, 2 weeks ago by Profile photo of Curtis Smith Curtis Smith.
Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic.