none
Need to change DNS Server for NICs which have Gateway confirgured RRS feed

  • Question

  • I want to change Secondary DNS for my environment (Approx 700 Servers). Each server have multiple NICs which include Production NICs , Backup NICs (dedicated to backup ,Do not have gateway configured). I am testing if I can change it with powershell on one Server so that I can do it for all servers. That is what I am trying. 

    $strComputer = "."

    $colItems = Get-WmiObject -class "Win32_NetworkAdapterConfiguration" -computername $strComputer | Where {$_.IPEnabled -Match "True"}

    foreach ($objItem in $colItems) {
       Write-Host "NIC NAME: " $objItem.Caption
       Write-Host "IPAddress: " $objItem.IPAddress
       Write-Host "IPEnabled: " $objItem.IPEnabled
       Write-Host "Gateway IP: " $objItem.DefaultIPGateway
       Write-Host "DNS Servers: " $objItem.DNSServerSearchOrder
       Write-Host ""

       $DNSServers="192.17.1.1","192.16.1.1"
       $objItem.SetDNSServerSearchOrder($DNSServers)
         }

    But it change the DNS  for all NICs, I want  to  change only for Production  NIC which has gateway ( NIC name is not Production on each server, so I can not just filter with name. 

    I am trying to introduce a if statement in the for loop so that it can verify that gateway is configured or not but I am unsucessful.

    Any Idea.

    Appreciate all the suggestion and help.

    Sunday, March 29, 2015 10:55 PM

Answers

  • Try it this way:


    $dnsServers = "192.17.1.1","192.16.1.1"
    
    get-wmiobject Win32_NetworkAdapterConfiguration -filter "IPEnabled=True" |
      where-object { $_.DefaultIPGateway } | foreach-object {
        $_.SetDNSServerSearchOrder($dnsServers)
      }
    


    -- Bill Stewart [Bill_Stewart]

    Sunday, March 29, 2015 11:37 PM
    Moderator

All replies

  • Try it this way:


    $dnsServers = "192.17.1.1","192.16.1.1"
    
    get-wmiobject Win32_NetworkAdapterConfiguration -filter "IPEnabled=True" |
      where-object { $_.DefaultIPGateway } | foreach-object {
        $_.SetDNSServerSearchOrder($dnsServers)
      }
    


    -- Bill Stewart [Bill_Stewart]

    Sunday, March 29, 2015 11:37 PM
    Moderator
  • Unfortunately there can be only one set of DNS servers for an host.  There can be only one name resolver. The  router cannot choose and interface (route) until it has resolved the IP of the name.  Ask your self what would the system do if you said "connect me to google.com"? How would it know which interface you wanted to use for DNS>


    ¯\_(ツ)_/¯

    Sunday, March 29, 2015 11:48 PM