none
Change DNS only on NIC's with a certain IP RRS feed

  • Question

  • In my enviroment i have several forests that are setup as different domains. We are currently in the process of upgrading our DC's to 2012, these will also be my DNS servers. My servers have different NIC's that go to different subnets. I need to change the DNS servers only on the NIC's that have a certain DNS server on them. I am changing the dns on several servers from the DC and using a txt file for my server list. I am trying to use an IF statement in my script to define the old DNS server and tell the script to only change DNS servers on these NIC's. I do not want to change the DNS on the other NIC's because they go to different subnets, some like my backup network do not even use DNS servers. The script as i have it will change all the DNS servers on all the NIC's on the servers. Why is my IF statement not working?

    Thanks in advance for your time and assistance.

    $OldDNS = 192.168.10.47 
    $computer = get-content C:\computers\computers.txt
     $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer |where{$_.IPEnabled -eq “TRUE”}
       Foreach($NIC in $NICs) {if($_.DNSServerSearchOrder -match $OldDNS ){$_.DNSServerSearchOrder}
     
    $DNSServers = “198.168.10.125",”198.168.10.126"
      $NIC.SetDNSServerSearchOrder($DNSServers)
      $NIC.SetDynamicDNSRegistration(“TRUE”)
     }


    Thanks for your help

    Friday, August 29, 2014 7:19 PM

Answers

  • I think this is closer to what you want:

    $OldDNS ='192.168.10.47 '
    $DNSServers = '198.168.10.125','198.168.10.126'
    $computer = get-content C:\computers\computers.txt
    Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer -filter 'IPEnabled=True' |
         Foreach-Object{
              if($_.DNSServerSearchOrder -match $OldDNS ){
                   $_.SetDNSServerSearchOrder($DNSServers)
                   $_.SetDynamicDNSRegistration($true)
              }
         }
    


    ¯\_(ツ)_/¯

    • Marked as answer by jthomas777 Friday, August 29, 2014 8:33 PM
    Friday, August 29, 2014 8:11 PM

All replies

  • $OldDNS needs to be quoted as in:

    $OldDNS = "192.168.10.47"


    Sam Boutros, Senior Consultant, Software Logic, KOP, PA http://superwidgets.wordpress.com (Please take a moment to Vote as Helpful and/or Mark as Answer, where applicable)


    • Edited by Sam Boutros Friday, August 29, 2014 7:36 PM
    Friday, August 29, 2014 7:35 PM
  • I think this is closer to what you want:

    $OldDNS ='192.168.10.47 '
    $DNSServers = '198.168.10.125','198.168.10.126'
    $computer = get-content C:\computers\computers.txt
    Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer -filter 'IPEnabled=True' |
         Foreach-Object{
              if($_.DNSServerSearchOrder -match $OldDNS ){
                   $_.SetDNSServerSearchOrder($DNSServers)
                   $_.SetDynamicDNSRegistration($true)
              }
         }
    


    ¯\_(ツ)_/¯

    • Marked as answer by jthomas777 Friday, August 29, 2014 8:33 PM
    Friday, August 29, 2014 8:11 PM
  • Thank you both. I used the -filter instead of the where statement and built the array.

    Thanks for your help

    Friday, August 29, 2014 8:33 PM