- ' This script changes the DNS servers for a network device in Windows
- ' to a defined IP or deletes them and makes it the default automatic
- ' Test if in admin mode by detecting 'elevated' in the command line that launched this script
- ' WScript is basically a THIS script object
- If WScript.Arguments.Named.Exists("elevated") = False Then
- 'Launch this script again as administrator
- CreateObject _
- ("Shell.Application").ShellExecute _
- "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
- WScript.Quit
- Else
- 'Change the working directory from the system32 folder back to the script's folder.
- Set oShell = CreateObject("WScript.Shell")
- oShell.CurrentDirectory = CreateObject _
- ("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
- 'MsgBox "Now running with elevated permissions"
- End If
- ' Define the DNS Primary and secondary server IPs
- DNSserv = "111.111.111.111,222.222.222.222"
- ' Ask if the DNS servers should be set or removed
- iAction = MsgBox("Would you like to use DNS?", _
- vbYesNoCancel+vbQuestion+vbApplicationModal, "DNS Toggle: " & DNSserv)
- ' Cancel was selected - quit this script
- If vbCancel = iAction Then
- WScript.Quit
- End If
- strComputer = "." ' This computer
- ' Get the Script shell and WMI Service objects
- Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
- Set WshShell = WScript.CreateObject("WScript.Shell")
- ' The registry key that hold the TCP/IP parameters
- Regkey = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters\Interfaces\"
- ' Enumerate all of the enabled network devices
- Set IPDevSet = objWMIService.ExecQuery _
- ("Select SettingID from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
- DNSservRead = ""
- For Each IPDev in IPDevSet
- ' Get the current setting
- DNSservRead = WshShell.RegRead _
- ( Regkey & IPDev.SettingID & "\NameServer")
- 'MsgBox "DNS Servers was set to:" & vbCrLf & DNSservRead, vbOK, "Old Status"
- ' if it's blank and the user chose YES set it to the DNS Server values.
- ' So we don't overwrite the values that are already set
- If "" = DNSservRead and vbYes = iAction Then
- 'Combine regkey, settingID, and nameserver to create the correct registry key path
- WshShell.RegWrite Regkey & IPDev.SettingID & "\NameServer", DNSserv, "REG_SZ"
- ' If the user selected NO to delete the DNS values this script sets
- ' and it's not set to those DNS values do not mess with it.
- ElseIf DNSservRead = DNSServ and vbNo = iAction Then
- WshShell.RegWrite Regkey & IPDev.SettingID & "\NameServer", "", "REG_SZ"
- End If
- ' See the new setting
- 'DNSservRead = WshShell.RegRead( Regkey & IPDev.SettingID & "\NameServer")
- 'MsgBox "DNS Servers now set to:" & vbCrLf & DNSservRead, vbOK, "New Status"
- Next
- 'Final message
- szTitle = ""
- szMssg = ""
- If vbYes = iAction Then
- szMssg = "DNS Servers now set to use:" & vbCrLf & " " & DNSserv
- szTitle = "DNS.com is ON"
- Else
- szMssg = "DNS Servers deleted."& vbCrLf & "Now set to Automatic"
- szTitle = "DNS.com is OFF"
- End If
- MsgBox szMssg, vbInformation, szTitle
来源: http://www.phpxs.com/code/1008844/