ksbunger1
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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfig uration 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 (strIPAddr ess, 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
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfig
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
errGateways = objNetAdapter.SetGateways(
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
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
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
ASKER
vladh,
We just need to assign and delete secondary IP addresses to existing NICs using WMI. No need to add a gateway.
Thanks
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfig uration where IPEnabled=TRUE")
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
Next
-------------------------- ---------- ----
Vlad
below is the code to enable DHCP:
--------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfig
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableDHCP()
Next
--------------------------
Vlad
This is what DHCP is for ?????????