POPTEST老李分享修改dns ip的vbs代码
生活随笔
收集整理的這篇文章主要介紹了
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 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轉載于:https://www.cnblogs.com/poptest/p/5210592.html
總結
以上是生活随笔為你收集整理的POPTEST老李分享修改dns ip的vbs代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Solr入门和实践以及我对Solr的8点
- 下一篇: vmware下linux虚拟机传文件解决