在之前的博文中写过一个类似的脚本,但脚本并不完美。 近期又有类似的需求,懒得改了,直接写个新的。
针对hyper-v的多网卡,虚拟网卡,同样有效。
$nic_array =@{}
$nic_info = Get-NetAdapter
$nic_name = $nic_
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){
Set-DnsClientServerAddress -InterfaceIndex $i -ServerAddresses $new_dns_servers
}
测试一下:
-
未执行脚本前
-
执行后