Link to home
Start Free TrialLog in
Avatar of ksbunger1
ksbunger1Flag for United States of America

asked on

automation of IP Addresses to NIC with Subnet mask - VBScript

I am needing a VBScript code that I can automate an addition of IP Addresses and Subnet mask to a network adapter. I would also like to be able to delete if needed. I am currently using the following code, but it will only "change" the IP address and Subnet, I need to create more.

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

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

strIPAddress = Array("192.168.1.29")
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
Avatar of trinak96
trinak96

Although i dont know the answer to this i would be interested to know why you need it ?
This is what DHCP is for ?????????
Avatar of ksbunger1

ASKER

I am needing this for a backup script, for some servers we have.
ksbunger1:
could you please clarify what you are trying to do... Are you trying to automate "complete" IP configuration assignments such as DNS servers and lookup zones? Are you trying to assign secondary IP addresses to existing NICs? What's your criteria for selecting the NIC to configure (todays servers usually have more than one network card built-in)... I have a fair amount of functional VB code that manages IP configuration; it may help you depending on what you are trying to do....
Thanks
Vlad
vladh,

We just need to assign and delete secondary IP addresses to existing NICs using WMI. No need to add a gateway.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of vladh
vladh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That sounds good, How would I go about deleting them if need be?
You can either switch to DHCP or overwrite existing addresses with new ones. The script I gave you as an example should be modified to only work on the active NIC that you are using; the code will try to change ALL network adapters in the machine including dial-up and wireless adapters (you probably don't want to touch them).  One of the ways to exclude adapters you don't want to modify is to search for "wireless", "WAN", "Virtual", "Dial-Up" etc in the adapters' "caption" string

below is the code to enable DHCP:
-------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
 
For Each objNetAdapter In colNetAdapters
    errEnable = objNetAdapter.EnableDHCP()
Next
----------------------------------------

Vlad