Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript Looping through Collection of Adapters to get IPConfigs

Posted on 2008-11-04
5
Medium Priority
?
1,531 Views
Last Modified: 2013-11-10
Can anyone explain or point us to a book, a website or other discussion that might help us understand why most of the TechNet scripts that loop through the collection of adapters on a machine also include a second loop such as the one below,  i=LBound(objAdapter.IPAddress) to UBound(objAdapter.IPAddress).  Doesn't each adapter get one and only one IP Address?  On our machines, each time we have tested, the value of i has been zero.   Is the second loop really necessary, if so where/when?

strComputer = "."
strFileName = "C:\test.txt"
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
     (strFileName, ForAppending, True)

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

     Set colAdapters = objWMIService.ExecQuery _
     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
     For Each objAdapter in colAdapters
     If Not IsNull(objAdapter.IPAddress) Then
        For i=LBound(objAdapter.IPAddress) to UBound(objAdapter.IPAddress)
             MyIP = objAdapter.IPAddress(i)
             objTextFile.WriteLine(MyIP)
             Next
        End If
     Next

objTextFile.Close
0
Comment
Question by:BITASCII
  • 2
  • 2
5 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22881708
Hi, the reason that the second loop is required might be better explained here:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0825.mspx
http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar07/hey0301.mspx

The second loop is required only because the IPAddress is stored as an array, whether that network adapter has one IP address assigned to it or not, that's all.  An array requires a For loop, or the Join function, to output correctly to a string.

You can run this code to see the IP Addresses assigned to your adapters.

Regards,

Rob.
     strComputer = "."
     Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
     Set colAdapters = objWMIService.ExecQuery _
     ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
 	strResults = "Network adapters and IPs:"
     For Each objAdapter in colAdapters
     If Not IsNull(objAdapter.IPAddress) Then 
        For i=LBound(objAdapter.IPAddress) to UBound(objAdapter.IPAddress)
             MyIP = objAdapter.IPAddress(i)
             strResults = strResults & VbCrLf & objAdapter.Caption & ": " & MyIP
        Next
        End If
     Next
     WScript.Echo strResults

Open in new window

0
 
LVL 4

Expert Comment

by:Chizl
ID: 22900718
Just an FYI, you can have 256 IP Addresses per NIC.
0
 

Author Comment

by:BITASCII
ID: 22901784
Hey guys really appreciate the answers and maybe this is the wrong place to have asked why it is that in WMI objAdapter.IPAddress is an array.  Admitedly our experience is limited to devices we deal with in a networked office environment, and clearly we're not aware of other TCP/iP applications.  So from a technical point of view, we understand that the object in question is an array and therefore needs a second loop to traverse the array elements.  We had hoped that by posting our code, it would help make it clear that the question is a practical one and becuase se we just haven't read about and can't imagine a nework adapter config taking on several IP values.  In our (admittedly limited) experience, when we poke around the WMI Classes we notice various devices in a machine (the RAS asynch adaptor, the WAN port, the 1394 NetAdapter, and of course the ethernet cards) each create another instance of Win32_NetworkAdapterConfiguration.  But then, each of the instances holds just one IP vlaue.  And,  every time we've taken a reading of the variable "i" (see our or Rob's code above), the value 's been zero --  we just have never seen any of the additional 256 possible elements populated -- and just couldn't imagine who would need to associate several IPs with an adaptor config.  We'll read on and hopefully figure out who the designers were, and what they had in mind. We were hoping an expert knew of an FRP, a white paper or article for the layman  that could explain why there can be 256 IP values, what did the architects have in mind!  That is so much versatility and power, it is mind boggling how much processing power would be needed to use that many connections.  Anyway, books are in transit from Amazon and elesehwere... we'll read on ... and one of these days when the answer is very obvious we might be ashamed of bugging experts, posting this question here.  But as of now, to us newbies (We're newbies to the field, but especialy to this forum), the practical applications of this design  (if any) lie far beyond our imagination, we're dying to find out what the practical purpose of the design may have been.  Begging forgiveness if the question is too general for the forum, again, we're newbies.  THANKS!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 375 total points
ID: 22901900
Hi, well, from here:
http://www.microsoft.com/technet/scriptcenter/topics/networking/04_atnc_ipadd.mspx
under the
Configuring a Static IP Address

section, it states that a network adapter configured as Static can have multiple IP addresses.

If you've got a statically assigned IP Address on a network card, go to Control Panel, go to Network Connections then choose your ethernet connection. Go to its properties, double click on Internet Protocol(TCP/IP) and click on Advanced... .This is where you can assign more IP addresses

Much like this:
http://technet.microsoft.com/en-us/library/cc722518.aspx

Regards,

Rob.
0
 

Author Comment

by:BITASCII
ID: 22905499
Thanks Rob, The intro to the second link you provided answers where multiple IP for the same NIC can be useful ~ below are the relevant paragraphs.  Thanks again for the pointer!

Multiple IP addresses are useful in several situations:

You want a single computer to appear to be several different computers. For example, if you're installing an intranet server, you may also want the server to provide Web, FTP, and SMTP services. You can use a different IP address for each service, and you can use different IP addresses for the intranet and the FTP services.

If your network is divided into multiple logical IP networks (subnets) and the computer needs access to these subnets to route information or provide other internetworking services, you may want a single network adapter card to have multiple IP addresses. For example, the address 192.55.10.8 could be used for workstations accessing a server from the 192.55.10.0 subnet and the address 192.55.11.8 could be used for workstations accessing a server from the 192.55.11.0 subnet.

Caution: When you use a single network adapter, IP addresses must be assigned to the same network segment or segments that are part of a single logical network. If your network is divided into multiple physical networks, you must use multiple network adapters, with each network adapter being assigned an IP address in a different physical network segment.

0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Screencast - Getting to Know the Pipeline

577 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