1 ############################ script functions and instructions ###### ############# 2 # configure the DNS server IP address on the local server 3 # Creation Time: 4 5 ########################## define a function ########## ################## 6 # define a function: configure DNS 7 # onid as the nic id; $ dnsservers is the IP address list of the DNS server to be configured 8 function dnsconfig ($ connectionid, [array] $ dnsservers) 9 {10 # determine whether the current system is 2003 (5) or 2008. The commands used when setting the DNS server are different 11 if (gwmi win32_operatingsystem ). version. split (". ") [0]-EQ 5) 12 {13 netsh.exe interface IP Set DNS "$ connectionid" static $ dnsservers [0] primary14 $ 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 = no19 $ dnsservers [1 .. $ dnsservers. count] | % {netsh.exe interface IP add dnsservers "$ connectionid" $ _ validate = No} 20} 21} 22 23 ################ script start ############### ######################################## ############## 24 $ Index = (gwmi win32_networkadapterconfiguration |? {$ _. Defaultipgateway-ne $ null }). index25 [array] $ IP = (gwmi win32_networkadapterconfiguration-filter "Index = $ Index "). ipaddress26 $ connectionid = (gwmi win32_networkadapter-filter "Index = $ Index "). netconnectionid 27 # Set dns28 $ ip2 = $ IP [0] According to the IP address that belongs to the IDC. 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 $ dnsservers33} 34 elseif ($ ip2-EQ "10.5") 35 {36 $ dnsservers = "10.5.2.220", "192.168.251.210 ", "10.4.2.220" 37 dnsconfig $ connectionid $ dnsservers38} 39 else 40 {41 $ dnsservers = "192.168.251.210", "10.4.2.220 ", "10.5.2.220" 42 dnsconfig $ connectionid $ dnsservers43} 44 45 # view local DNS Server LIST 46 $ DNSS = gwmi win32_networkadapter-filter "netconnectionstatus = 2" | foreach {(gwmi win32_networkadapterconfiguration-filter" index = $ ($ _. index )"). dnsserversearchorder} 47 $ DNSS
Configure the DNS server IP Address