Help understanding code to get IP Address of computer

tupac1979
tupac1979 used Ask the Experts™
on
I already know how to get the IP address of a machine using VBscript I just dont understand why this function does work and the code below doesnt work I just need some help with understanding it. The code below returns the IP address of a network adapter that is static:

Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
    If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
         WScript.Echo "  IP address:        " & objAdapter.IPAddress(i)
      Next

But this code does not work below and I would like to why? Thanks
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
         WScript.Echo "  IP address:        " & objAdapter.IPAddress
      Next
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
The IP Address is an Array, and so needs to be iterated through, instead of just outputting the value directlly.  Any on these links should help you understand the properties of the Win32_NetworkAdapterConfiguration:
Why am I Getting an Error when Trying to Determine an IP Address?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug04/hey0825.mspx
Win32_NetworkAdapterConfiguration
http://msdn2.microsoft.com/en-us/library/aa394217.aspx
How Can I Change the First Two Octets of an IP Address?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar07/hey0301.mspx

Regards,

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial