none
Script to change DNS on remote windows 2008 R2 servers. RRS feed

  • Question

  • Good afternoon,

    I am trying to build a script to remotely change the DNS servers we are using on our remote servers. I have gotten stuck though. I have come up with this to get the Alias of the NICs listed:

    $server = "servername"
    $interfaces = Get-WmiObject Win32_NetworkAdapter -ComputerName $server
    $interfaces | ForEach{
        $aliasname = $_ | ForEach-Object { $_.NetConnectionID }
        $name = $_.GetRelated("Win32_PnPEntity") | Select-Object -ExpandProperty Name
        foreach($name in $aliasname){
            Get-WmiObject Win32_NetworkAdapterConfiguration -computername $server | select $name, Description, IPAddress, DNSServerSearchOrder |  Format-Table -AutoSize
        }
    }
    but the problem is I only want to change the DNS of one NIC and the NICs on the different servers may have a similar name but not always exactly the same. It may also not always be the same number NIC. I wanted to use something to exclude the wrong NIC so I tried something like 

        foreach($name in $aliasname | Where-Object -Property $name -contains "WhatIAmLookingFor"){
            Get-WmiObject Win32_NetworkAdapterConfiguration -computername $server | select $name, Description, IPAddress, DNSServerSearchOrder |  Format-Table -AutoSize
        }
    }

    but it still returns both NICs. I have tried quite a few ways to make it exclude the NIC I don't want to change and only include the one I want but I usually either get nothing back or get both NICs or it errors out because parameters are wrong. Any help with this issue is very much appreciated.


    _Sam_

    Tuesday, June 18, 2019 5:59 PM

All replies

  • This will always get the enabled NIC.

    Changing the DNS server search order on any one NIC changes it for the whole server.   The choice of which NIC to use is done by the router and not the NIC.

    Get-WmiObject Win32_NetworkAdapterConfiguration-Filter 'IpEnabled=True' -computername $server


    \_(ツ)_/

    Tuesday, June 18, 2019 6:43 PM
  • Right but more than one NIC may be enabled. Some of the servers are using two NICs one for our network and one to connect to another network. So I need to exclude the one on the other network.

    _Sam_

    Tuesday, June 18, 2019 6:52 PM
  • So just change the first NIC.

    $nic = @(Get-WmiObject Win32_NetworkAdapterConfiguration-Filter 'IpEnabled=True' -computername $server)[0]


    \_(ツ)_/

    Tuesday, June 18, 2019 6:58 PM
  • PS C:\Windows\system32> $server = "servername"
    $nic = @(Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'IpEnabled=True' -computername $server)[0]
    $interfaces = Get-WmiObject Win32_NetworkAdapter -ComputerName $server
    $interfaces | ForEach{
        $aliasname = $_ | ForEach-Object { $_.NetConnectionID }
        $name = $_.GetRelated("Win32_PnPEntity") | Select-Object -ExpandProperty Name
        foreach($name in $aliasname){
            $nic | select $name, Description, IPAddress, DNSServerSearchOrder |  Format-Table -AutoSize
        }
    }
    
    My Company Domain NIC#1 Description                         IPAddress        DNSServerSearchOrder          
    ----------------------- -----------                         ---------                                 --------------------          
                            Broadcom NetXtreme Gigabit Ethernet {IP ADDRESS} {DNS SERVER ADDRESS}
    
    
    
    Hospital Domain NIC#2 Description                           IPAddress           DNSServerSearchOrder          
    ------------ -----------                         ---------                                 --------------------          
                          Broadcom NetXtreme Gigabit Ethernet   {IP ADDRESS}        
    This still gives me both NICs unfortunately.

    _Sam_

    Tuesday, June 18, 2019 7:15 PM
  • You do not  need any of that code.  Just use the code I posted.  Nothing else is required.


    \_(ツ)_/

    Tuesday, June 18, 2019 7:17 PM
  • OK but now it goes back to giving me everything except the DNS servers.

    DHCPEnabled      : False
    IPAddress        : {IP ADDRESS}
    DefaultIPGateway : {IP ADDRESS}
    DNSDomain        : 
    ServiceName      : b57nd60a
    Description      : Broadcom NetXtreme Gigabit Ethernet
    Index            : 7
    

    As well sometimes the first NIC is the hospital domain NIC not my company domain NIC which is why I was trying to get the alias name before and have it only use NICs which Alias contained my companies name.


    _Sam_

    Tuesday, June 18, 2019 7:56 PM
  • I think you need to stop guessing nd take time to learn basic PowerShell.

    $nic = @(Get-WmiObject Win32_NetworkAdapterConfiguration-Filter 'IpEnabled=True' -computername $server)[0]
    $nic.DnsServerSearchOrder

    I suggest learning WMI and how to use the WMI Cmdlets with WMI.  You also need to learn a bit about networking. Put all of that together with a basic understanding of PowerShell and you will be able to do this.

    The following link will get you started with PowerShell, WMI and some basics of automation of Windows tasks.

    Microsoft Virtual   Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Tuesday, June 18, 2019 8:38 PM
  • Another place to learn about how to use these APIs is to look up the class you are trying to use.

    If you search for this  "Win32_NetworkAdapterConfiguration" you will find the class documentation for the WMI class.  This will help you to understand what it is and how it is used.

    https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkadapterconfiguration

    As You learn how to use Windows technically and not in a GUI program you will find that it is necessary for you to research the items you want to use or configure.  The Microsoft documentation is extensive and can help you to understand the technology and how it is used.  It will also help you to learn the nomenclature used in Windows technologies and in most of the computer world.  The GUI is designed to hide all of this and just supply a simplified recipe and lots of visuals to help doing the tasks presented in the GUI.


    \_(ツ)_/

    Tuesday, June 18, 2019 10:24 PM