1 #############################脚本功能及说明#################### 2 #该脚本用来在本地服务器上配置DNS服务器IP 3 #创建时间:2014-10-22 4 5 #########################定义函数############################# 6 #定义函数:配置DNS 7 #ConnectionID为网卡ID;$dnsservers为要配置的DNS服务器的IP列表 8 function DNSConfig($ConnectionID,[array]$dnsservers) 9 { 10 #判断当前系统是2003(5),还是2008,在设置DNS服务器的时候所使用的命令不同 11 If ((gwmi win32_operatingsystem).version.split(".")[0] -eq 5) 12 { 13 netsh.exe interface ip set dns "$ConnectionID" static $dnsservers[0] primary 14 $dnsservers[1..$dnsservers.count] | % { netsh.exe interface ip add dns "$ConnectionID" $_ } 15 } 16 Else 17 { 18 netsh.exe interface ip set dnsservers "$ConnectionID" static $dnsservers[0] primary validate=no 19 $dnsservers[1..$dnsservers.count] | % { netsh.exe interface ip add dnsservers "$ConnectionID" $_ validate=no } 20 } 21 } 22 23 ################脚本开始###################################################################### 24 $index = (gwmi Win32_NetworkAdapterConfiguration |?{ $_.DefaultIPGateway -ne $null}).Index 25 [array]$ip = (gwmi Win32_NetworkAdapterConfiguration -filter "index = $index").IPAddress 26 $ConnectionID = (gwmi win32_networkadapter -filter "index = $index").NetConnectionID 27 #根据IP属于IDC设置DNS 28 $ip2 = $ip[0].split(".")[0,1] -join "." 29 If ($ip2 -eq "10.4") 30 { 31 $dnsservers="10.4.2.220","10.5.2.220","192.168.251.210" 32 dnsconfig $ConnectionID $dnsservers 33 } 34 Elseif ($ip2 -eq "10.5") 35 { 36 $dnsservers="10.5.2.220","192.168.251.210","10.4.2.220" 37 dnsconfig $ConnectionID $dnsservers 38 } 39 Else 40 { 41 $dnsservers="192.168.251.210","10.4.2.220","10.5.2.220" 42 dnsconfig $ConnectionID $dnsservers 43 } 44 45 #查看本地DNS服务器列表 46 $dnss=gwmi win32_networkadapter -filter "NetConnectionStatus = 2"|foreach {(gwmi Win32_NetworkAdapterConfiguration -filter "index = $($_.index)").DNSServerSearchOrder} 47 $dnss