POPTEST老李分享修改dns ip的vbs代码
POPTEST老李分享修改dns ip的vbs代码
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-84505200。
准备把学员在工作中写的一个自动化功能加入到培训中案例,由于测试服务器功能,每次要修改ip地址,所以要自动化去修改dns ip,代码如下:
' 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 objectIf 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.QuitElse '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 IPsDNSserv = "111.111.111.111,222.222.222.222"' Ask if the DNS servers should be set or removediAction = MsgBox("Would you like to use DNS?", _ vbYesNoCancel+vbQuestion+vbApplicationModal, "DNS Toggle: " & DNSserv)' Cancel was selected - quit this scriptIf vbCancel = iAction Then WScript.QuitEnd IfstrComputer = "." ' This computer' Get the Script shell and WMI Service objectsSet objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set WshShell = WScript.CreateObject("WScript.Shell")' The registry key that hold the TCP/IP parametersRegkey = "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters\Interfaces\"' Enumerate all of the enabled network devicesSet 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 messageszTitle = ""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 IfMsgBox szMssg, vbInformation, szTitle

浙公网安备 33010602011771号