配置DNS服务器IP

时间:2022-06-01 20:52:06
 #############################脚本功能及说明####################
#该脚本用来在本地服务器上配置DNS服务器IP
#创建时间:2014-10-22 #########################定义函数#############################
#定义函数:配置DNS
#ConnectionID为网卡ID;$dnsservers为要配置的DNS服务器的IP列表
function DNSConfig($ConnectionID,[array]$dnsservers)
{
#判断当前系统是2003(5),还是2008,在设置DNS服务器的时候所使用的命令不同
If ((gwmi win32_operatingsystem).version.split(".")[0] -eq 5)
{
netsh.exe interface ip set dns "$ConnectionID" static $dnsservers[0] primary
$dnsservers[1..$dnsservers.count] | % { netsh.exe interface ip add dns "$ConnectionID" $_ }
}
Else
{
netsh.exe interface ip set dnsservers "$ConnectionID" static $dnsservers[0] primary validate=no
$dnsservers[1..$dnsservers.count] | % { netsh.exe interface ip add dnsservers "$ConnectionID" $_ validate=no }
}
} ################脚本开始######################################################################
$index = (gwmi Win32_NetworkAdapterConfiguration |?{ $_.DefaultIPGateway -ne $null}).Index
[array]$ip = (gwmi Win32_NetworkAdapterConfiguration -filter "index = $index").IPAddress
$ConnectionID = (gwmi win32_networkadapter -filter "index = $index").NetConnectionID
#根据IP属于IDC设置DNS
$ip2 = $ip[0].split("\.")[0,1] -join "."
If ($ip2 -eq "10.4")
{
$dnsservers="10.4.2.220","10.5.2.220","192.168.251.210"
dnsconfig $ConnectionID $dnsservers
}
Elseif ($ip2 -eq "10.5")
{
$dnsservers="10.5.2.220","192.168.251.210","10.4.2.220"
dnsconfig $ConnectionID $dnsservers
}
Else
{
$dnsservers="192.168.251.210","10.4.2.220","10.5.2.220"
dnsconfig $ConnectionID $dnsservers
} #查看本地DNS服务器列表
$dnss=gwmi win32_networkadapter -filter "NetConnectionStatus = 2"|foreach {(gwmi Win32_NetworkAdapterConfiguration -filter "index = $($_.index)").DNSServerSearchOrder}
$dnss