Set static IP (VBScript)

This code can be found in Chapter 1 of Windows Server 2003 Networking Recipes

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Windows Server 2003 Networking Recipes"

' This code sets the local IP address
'   to a static IP of 10.0.0.100,
'   with a subnet mask of 255.0.0.0,
'   a default gateway of 10.0.0.1,
'   and a metric of 1
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strIPAddress = Array("10.0.0.100")
strSubnetMask = Array("255.0.0.0")
strGateway = Array("10.0.0.1")
strGatewayMetric = Array(1)
' --------- END CONFIGURATION ------

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

Set adapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each a in adapters
    errIP = a.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = a.SetGateways(strGateway, strGatewaymetric)
    If errIP = 0 Then
        WScript.Echo "Success! The IP address has been changed."
    Else
        WScript.Echo "Error! The IP address could not be changed."
    End If
Next

This code has been viewed 3381 times.

New from the creators of TechTasks.com: StatSheet.com