0
点赞
收藏
分享

微信扫一扫

批量服务器更新DNS的powershell脚本

全栈顾问 2023-05-25 阅读 63

在之前的博文 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
  }

测试一下:

  • 未执行脚本前 image.png

  • 执行后 image.png

举报

相关推荐

0 条评论