Configure Static IP Address

Quickly set up a static IP address with this script.

Want to share a script? Click here to contribute!

Author: Shane Boudreaux - sboudrea [at] tulane [dot] edu

Platform:
Windows

Type:
Windows Script

Description:

If you normally have a static IP address but have to use DHCP at home or in other places, it is quite frustrating to have to go through all of the screens to acomplish this goal.

I've written this script, which will change to a static IP address quickly. There are 2 options when you execute the script: 1) assign an address that is hard-coded in the script, OR 2) prompt you for the address, mask, and GW.

If you commonly use the same static address, it's very easy to simply double-click the script and change your IP to your normal address!

Scroll down to view the script.


Configure Static IP Address


strComputer = "."
strAnswer=msgbox ("Use 192.168.1.141 / 24?" & vbcrlf & vbcrlf & "If not, you''ll be prompted to specify", vbYesNoCancel)

if strAnswer = vbYes then

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colNetAdapters = objWMIService.ExecQuery _
			("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
	strIPAddress = Array("192.168.1.141")
	strSubnetMask = Array("255.255.255.0")
	strGateway = Array("192.168.1.100")
	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 not be changed."
		End If
	Next

elseif strAnswer = vbNo then

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colNetAdapters = objWMIService.ExecQuery _
			("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
	strIPAddress = array(inputbox("IP Address"))
			'' Array("192.168.1.141")
	strSubnetMask = array(inputbox("Mask"))
			'' Array("255.255.255.0")
	strGateway = array(inputbox("GW"))
			'' Array("192.168.1.100")
	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 not be changed."
		End If
	Next


else
	
	msgbox ("Operation Cancelled by User")

end if

Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as - is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.





0 Comments (click to add your comment)
Comment and Contribute

 


(Maximum characters: 1200). You have characters left.