Code to set static IP and DNS server addresses with VBS _VBS

Source: Internet
Author: User

Core code One:

strIPAddress = Array ("192.168.0.148") 
Strsubnetmask = Array ("255.255.255.0") 
Strgateway = Array ("192.168.0.1 ") 
strgatewaymetric = Array (1) 
arrdnsservers = Array (" 192.168.0.1 "," 192.168.0.2 ") 
StrComputer =". " 

Set objWMIService = GetObject ("winmgmts:" _ 
& "{impersonationlevel=impersonate}!\\" & strComputer & "\ root\cimv2 ") 

Set colnetadapters = objWMIService.ExecQuery _ 
(" Select * FROM Win32_NetworkAdapterConfiguration where Ipenabled=true ") for each 

objnetadapter in colnetadapters 
errenable = objnetadapter.enablestatic ( strIPAddress, strsubnetmask) 
errgateways = Objnetadapter.setgateways (Strgateway, Strgatewaymetric) 
Errdns = Objnetadapter.setdnsserversearchorder (arrdnsservers) 
If errenable = 0 Then 
WScript.Echo "The IP Address has been changed. " 
Else 
WScript.Echo "The IP address could is not changed." 
End If 

Remember to change the parameters to what you want.

Code two:

Set the computer's IP address to 192.168.1.111, and set the IP gateway to 192.168.1.1.

StrComputer = "." 
Set objWMIService = GetObject ("winmgmts:\" & StrComputer & "Ootcimv2") 
set colnetadapters = Objwmiservice.ex Ecquery _ 
("select * from Win32_NetworkAdapterConfiguration where ipenabled=true") 
stripaddress = Array (" 192.168.1.111 ") 
Strsubnetmask = Array (" 255.255.255.0 ") 
Strgateway = Array (" 192.168.1.1 ") 
Strgatewaymetric = Array (1) for each 
objnetadapter in colnetadapters 
errenable = objnetadapter.enablestatic ( strIPAddress, strsubnetmask) 
errgateways = Objnetadapter.setgateways (Strgateway, strgatewaymetric) 
If errenable = 0 Then 
WScript.Echo "The IP address has been changed." 
Else 
WScript.Echo "The IP address could is not changed." 
End If 
Next

Original: http://demon.tw/programming/vbs-modify-ip-dns-setting.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.