How to change IP, Gateway and DNS with VBScript

This script will configure an IP address, subnet mask, default gateway, and DNS servers for the specified connection.
10.1.1.1 is the default gateway. Change other setting accordingly.

 
' ------ INITIAL CONFIGURATION ------
strComputer = "."
strConnection = "Local Area Connection"
strIP = Array("10.1.1.50")
strMask = Array("255.255.255.0")
strGatewayIP = Array("10.1.1.1")
arrDNSServers = Array("4.1.1.1", "4.2.2.2")
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colNA = objWMI.ExecQuery("select * " & _
" from Win32_NetworkAdapter " & _
" where NetConnectionID = '" & strConnection & "'" )
for each objNA in colNA
set colNAConfig = objWMI.ExecQuery _
("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
objNA.DeviceID & "'} " & _
" WHERE resultClass = win32_NetworkAdapterConfiguration ")
for each objNAConfig in colNAConfig
intRC = objNAConfig.EnableStatic(strIP,strMask)
intRC2 = objNAConfig.SetGateways(strGatewayIP)
if intRC = 0 and intRC2 = 0 then
WScript.Echo "IP address configured for " & strConnection
elseif intRC = 1 or intRC2 = 1 then
WScript.Echo "You must reboot for the changes to take effect for " & _
strConnection
else
WScript.Echo "There was an error configuring IP for " & _
strconnection & ": " & intRC & " and " & intRC2
end if
next
next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set Nics = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each Nic In Nics
intSetDNSServers = Nic.SetDNSServerSearchOrder(arrDNSServers)
If intSetDNSServers = 0 Then
WScript.Echo " DNS Servers set!"
Else
WScript.Echo " Error setting DNS server info."
End If
Next

Moving your apps to Amazon or Miscrosoft Clouds?

We can help you analyze your existing infrastructure, identify the cost savings we can achieve by migrating to a cloud provider. We can then execute end-to-end migration plan of your infrastructure and bringing down your TCO.

Cloud Computing

Ready for IPv6 Migration?

The Internet is running out of the equivalent of phone numbers - familiar problem, non-trivial solution.

The world has to move to IPv6, with its 128-bit addresses. But that's easier said than done.

IPv6 Migration

Are you fluent in "Linux"?

Learn Linux from a leading expert and quickly master you Linux skills.

Learn how to simplify your workflow and increase your productivity using tips and techniques of the pros.

Ideal training for Corporate IT Beginners and Advanced IT Admins alike.

Corporate Linux Training

Who's Online

We have 7 guests and no members online