简体   繁体   中英

Get-WMIObject Win32_NetworkAdapterConfiguration - Looping through X number of DNS Servers for each NIC

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

}


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM