Problem solve Get help with specific problems with your technologies, process and projects.

Change your network adapter's IP information

To change a machine's TCP/IP settings, you can use a VBScript to modify the network adapter information on a computer.

Changing a machine's TCP/IP settings from the GUI usually involves a number of steps. This becomes tedious if you...

have to do it often, especially if the machine is part of a testbed network where you test different deployment scenarios.

Using the following VBScript, you can quickly and frequently modify the network adapter information on a computer. To use this script, type it into Notepad (with Word Wrap turned off) and save it with a .vbs extension, for instance, ChangeIP.vbs

Option Explicit
 
Dim NetworkAdapter, AdapterConfiguration 'Objects
Dim IPAddress, SubnetMask, Gateway, DNS 'String Arrays
Dim RetVal 'Integers
 
For Each NetworkAdapter In
GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapter")
If NetworkAdapter.AdapterType = "Ethernet 802.3" Then
For Each AdapterConfiguration In GetObject("winmgmts:").InstancesOf
("Win32_NetworkAdapterConfiguration")
If UCase(AdapterConfiguration.ServiceName) = UCase(NetworkAdapter.ServiceName) Then
IPAddress = Array("192.168.0.10")
SubnetMask = Array("255.255.255.0")
Gateway = Array("192.168.0.1")
DNS = Array("35.8.2.41")
 
RetVal = AdapterConfiguration.EnableStatic(IPAddress, SubnetMask)
If Not RetVal = 0 Then

WScript.Echo "Failure assigning IP/Subnetmask."
End If
RetVal = AdapterConfiguration.SetGateways(Gateway)

If Not RetVal = 0 Then
WScript.Echo "Failure assigning Gateway."
End If
RetVal = AdapterConfiguration.SetDnsServerSearchOrder
(DNS)
If Not RetVal = 0 Then
WScript.Echo "Failure assinging DNS search order."
End If
End If
Next
End If
Next

It is essential to modify the IP information in the following lines of the above script, as required by your environment:

IPAddress = Array("192.168.0.10")
SubnetMask = Array("255.255.255.0")
Gateway = Array("192.168.0.1")
DNS = Array("35.8.2.41")

For example, to change the IP address of a machine to 172.16.49.5 with subnet mask 255.255.0.0 and default gateway 172.16.47.3, replace those lines with these:

IPAddress = Array("172.16.49.5")
SubnetMask = Array("255.255.0.0")
Gateway = Array("172.16.47.3")

Also, note this statement: If NetworkAdapter.AdapterType = "Ethernet 802.3" Then

This is where the script checks the AdapterType, which in this script is listed as "Ethernet 802.3." Modify this line if you have a different networking environment.

Once these changes have been made to the script, create a shortcut to the script and double-click on the shortcut to run the script.

About the author: Rahul Shah currently works at a software firm in India, where he is a systems administrator maintaining Windows servers. He has also worked for various software firms in testing and analytics, and also has experiences deploying client/server applications in different Windows configurations.

More information on this topic:

This tip originally appeared on SearchWinComputing.

This was last published in August 2006

Dig Deeper on VoIP QoS and Performance

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

Hello
Thank you for this script. I am lookung for a script to change default gateway only so this is a good base.

But when i run it, i get a syntax error on line 7 (For Each NetworkAdapter In).

I don't see the problem there... Can you please help me?
Cancel

-ADS BY GOOGLE

SearchNetworking

SearchITChannel

Close