Thursday, March 28, 2024

Configure Static IP Address

Datamation content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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 & "rootcimv2")
	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 & "rootcimv2")
	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.

Subscribe to Data Insider

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more.

Similar articles

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Latest Articles