在之前的博文 https://blog.51cto.com/magic3/2514240中写过一个类似的脚本,但脚本并不完美。 近期又有类似的需求,懒得改了,直接写个新的。
针对hyper-v的多网卡,虚拟网卡,同样有效。
$nic_array =@{}
$nic_info = Get-NetAdapter
$nic_name = $nic_info.name
foreach($nic in $nic_name){
 
     $nic_index = (Get-NetAdapter -Name $nic).ifIndex  
    
     Get-NetIPAddress -InterfaceIndex $nic_index -AddressFamily IPv4
     if ($?){
       $nic_ip = (Get-NetIPAddress -InterfaceIndex $nic_index -AddressFamily IPv4).IPAddress
       if ($nic_ip -like "172.21.*.*") {
           $nic_array[$nic_index] = $nic_ip
       }
     }
     
   
 } 
 $new_dns_servers = "172.21.7.99","172.21.7.100"
  Foreach ($i in $nic_array.Keys){
  
       #Write-Output  $i
       Set-DnsClientServerAddress -InterfaceIndex $i -ServerAddresses $new_dns_servers
  }
测试一下:
- 
未执行脚本前  
- 
执行后  










