加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用于修改 DNS IP 地址的 VB Shell 脚本

发布时间:2020-12-17 07:59:52 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ' 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' T

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

' 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 & "rootcimv2")
Set WshShell = WScript.CreateObject("WScript.Shell")

' The registry key that hold the TCP/IP parameters
Regkey = "HKEY_LOCAL_MACHINESYSTEMControlSet001servicesTcpipParametersInterfaces"

' 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,"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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读