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"))
}
}