[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

automation of IP Addresses to NIC with Subnet mask - VBScript

Posted on 2007-08-09
7
Medium Priority
?
1,861 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:ksbunger1
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:trinak96
ID: 19662502
Although i dont know the answer to this i would be interested to know why you need it ?
This is what DHCP is for ?????????
0
 

Author Comment

by:ksbunger1
ID: 19663328
I am needing this for a backup script, for some servers we have.
0
 
LVL 3

Expert Comment

by:vladh
ID: 19663856
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ksbunger1
ID: 19664352
vladh,

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

Thanks
0
 
LVL 3

Accepted Solution

by:
vladh earned 1500 total points
ID: 19664704
try this:
'-------------------------------8<------------------------------------------
on error resume next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

strIPAddress = Array("137.15.42.100","192.168.1.141")
strSubnetMask = Array("255.255.255.0","255.255.255.0")
strGateway = Array("137.15.42.1")
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
set objWMIService = nothing
'---------------------------------------------------------8<----------------------------------

vlad
0
 

Author Comment

by:ksbunger1
ID: 19665260
That sounds good, How would I go about deleting them if need be?
0
 
LVL 3

Expert Comment

by:vladh
ID: 19665634
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question