0

For a group of computers, I want to connect to each one, enumerate each NIC that has a real IP, and for each NIC I want to loop through each DNS server (first, second, third, fourth, fifth etc) and if it contains a specific IP (e.g. 8.8.8.8) I want to replace it (with 7.7.7.7) without affecting any other DNS Servers. How do I do this for X number of DNS Servers without doing each server entry one at a time (like I have done below for the first 3) and change it at the same time.

$computer ='wrk-01'
$NICs     = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | 
            where{$_.IPEnabled -eq "TRUE"}

Foreach($NIC in $NICs) {

  #  $DNSServerCount = $Nic.DNSServerSearchOrder.Count
  #  $DNSServerArray = @($nic.DNSServerSearchOrder[$DNSServerCount - $DNSServerCount,$DNSServerCount - $DNSServerCount + 1,$DNSServerCount - $DNSServerCount + 2])
   # write-host $nic.description
 #   write-host $nic.PSComputerName $DNSServerArray

    if($nic.dnsserversearchorder[0] -like "8.8.8.8"){   
    write-host "matched 8.8.8.8 at position 0"
    $NIC.SetDNSServerSearchOrder(@("7.7.7.7", $nic.dnsserversearchorder[1], $nic.dnsserversearchorder[2]))
    }

    if($nic.dnsserversearchorder[1] -like "8.8.8.8"){   
    write-host "matched 8.8.8.8 at position 1"
    $NIC.SetDNSServerSearchOrder(@($nic.dnsserversearchorder[0],"7.7.7.7", $nic.dnsserversearchorder[2]))
    }

        if($nic.dnsserversearchorder[2] -like "8.8.8.8"){   
    write-host "matched 8.8.8.8 at position 2"
    $NIC.SetDNSServerSearchOrder(@($nic.dnsserversearchorder[0], $nic.dnsserversearchorder[1],"7.7.7.7"))
    }

}


Aziz
  • 283
  • 1
  • 14

1 Answers1

1

Here's one way to check and update if there is a match. Once a match is found, it will replace any matching line with the desired replacement otherwise output, the collected list is then set as the new DNS servers. If none match, it will just continue to the next nic.

$computer ='wrk-01'
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | 
            where{$_.IPEnabled -eq "TRUE"}

Foreach($NIC in $NICs)
{
    $DNSIPs = $nics.DNSServerSearchOrder
    if($DNSIPs -contains '8.8.8.8')
    {
        Write-Host "Match found... updating" -ForegroundColor Green
        $NewDNS = $DNSIPs | foreach {
            $_ -replace '8.8.8.8','7.7.7.7'
        }
        $null = $NICs.SetDNSServerSearchOrder($NewDNS)
    }
}

One recommendation that may save you some headache is to not use plural variable names, but instead use a descriptive name like this.

$computer ='wrk-01'
$NICList = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer | 
            where{$_.IPEnabled -eq "TRUE"}

Foreach($NIC in $NICList)
{
    $DNSIPList = $nics.DNSServerSearchOrder
    if($DNSIPList -contains '8.8.8.8')
    {
        Write-Host "Match found... updating" -ForegroundColor Green
        $NewDNS = $DNSIPList | foreach {
            $_ -replace '8.8.8.8','7.7.7.7'
        }
        $null = $NICs.SetDNSServerSearchOrder($NewDNS)
    }
}
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
  • Thanks so much, works perfectly! I forgot about pipelining inside the script itself :) . BTW, the last line actually sets the DNS order, but what is the $null for? And thanks for tip on variable names. – Aziz Sep 08 '20 at 09:11
  • The $null just swallows the wmi output to keep the console clean. – Doug Maurer Sep 08 '20 at 11:56