• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 823
  • Last Modified:

Help with Nested For Next in a VBScript

I'm attempting to create a vbscript that uses  WMI queries to get the interface ID and the IP Address, stripping the IPv6 Address. As an example the script below outputs the following:

Interface1  10.10.10.10
Interface1  192.168.1.10
interface2  10.10.10.10
Interface2  192.168.1.10

I would like for the script to output the following:

Interface1 10.10.10.10
Interface2 192.168.1.10

I'm a novice as you can tell so any help would be appreciated.

Dim objWMI : Set objWMI = GetObject("winmgmts:")
 Dim colSettingsNetAdap : Set colSettings = objWMI.ExecQuery("Select * from Win32_NetworkAdapter Where NetEnabled =

'True'")
 Dim colSettingsNetAdapConfig : Set colSettingsNetAdapConfig = objWMI.ExecQuery("Select * from

Win32_NetworkAdapterConfiguration Where IPEnabled = 'True'")
 Dim Net, strNetID, IP
 For Each objNet in colSettings
   strNetID = objNet.NetConnectionID
     For Each objIP in colSettingsNetAdapConfig
       For i=LBound(objIP.IPAddress) to UBound(objIP.IPAddress)
       If InStr(objIP.IPAddress(i),":") = 0 Then Wscript.echo strNetID & space(3) & objIP.IPAddress(i)
       Next
   Next
 Next
0
crp0499
Asked:
crp0499
  • 4
  • 4
  • 2
2 Solutions
 
ZabagaRCommented:
I don't understand completely.

Why do you want the output for Interface 1 to be 10.10.10.10 but Interface 2 192.168.1.10?

What are you trying to do? Below you're showing 2 interfaces that each have 2 IPv4 addresses. But for your output, you want interface1 to be the top 10.10.10.10 entry and interface2 is the second entry 192.168.1.10? That's what I can't figure out.

Interface1  10.10.10.10
Interface1  192.168.1.10
interface2  10.10.10.10
Interface2  192.168.1.10
0
 
clockwatcherCommented:
Try
dim adapters
set adapters = Wscript.CreateObject("Scripting.Dictionary")

Dim objWMI
Set objWMI = GetObject("winmgmts:")

Dim colSettings
Set colSettings = objWMI.ExecQuery("Select * from Win32_NetworkAdapter Where NetEnabled = 'True'")

Dim colSettingsNetAdapConfig
Set colSettingsNetAdapConfig = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = 'True'")

dim adapter
for each adapter in colSettings
    adapters.add adapter.InterfaceIndex, adapter.NetConnectionID
next

dim i, config, connectionid
for each config in colSettingsNetAdapConfig
    connectionid = adapters(config.InterfaceIndex)
    for i = lbound(config.IPAddress) to ubound(Config.IPAddress)
        if instr(config.IPAddress(i),":") = 0 then wscript.echo connectionid & vbtab & config.IPAddress(i)
    next
next

Open in new window

0
 
ZabagaRCommented:
Here you go:

' List IP Addresses for a Computer


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

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            If Not Instr(IPConfig.IPAddress(i), ":") > 0 Then
                        WScript.Echo IPConfig.Caption(i), IPConfig.IPAddress(i)
         End If
        Next
    End If
Next
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
crp0499CEOAuthor Commented:
Clockwatcher, you are awesome! That is exactly what I was looking for. You are 2 for 2.  There is one thing, and this just may be an annoyance, but is there a way to align the IP addresses? The vbtab works for the first and third interface, let me show you an example:

Interface 2    10.10.10.10     <-----  this one's fine
iSCSI1    192.168.5.20  <----here is where it does not align
iSCSI2            192.168.10.20 <-----  This one's fine

I'm using it in BGInfo, if there is not a way to align them I will deal with it.
0
 
clockwatcherCommented:
No problem.  Can't really test this but it should pad things out and align them for you, assuming you're using a fixed-width font.

dim adapters
set adapters = Wscript.CreateObject("Scripting.Dictionary")

Dim objWMI
Set objWMI = GetObject("winmgmts:")

Dim colSettings
Set colSettings = objWMI.ExecQuery("Select * from Win32_NetworkAdapter Where NetEnabled = 'True'")

Dim colSettingsNetAdapConfig
Set colSettingsNetAdapConfig = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = 'True'")

dim adapter, maxlen
for each adapter in colSettings
    adapters.add adapter.InterfaceIndex, adapter.NetConnectionID
    if len(adapter.NetConnectionID) > maxlen then
        maxlen = len(adapter.NetConnectionID)
    end if
next

dim i, config, connectionid, padding
for each config in colSettingsNetAdapConfig
    connectionid = adapters(config.InterfaceIndex)
    padding = maxlen - len(connectionid) + 3
    for i = lbound(config.IPAddress) to ubound(Config.IPAddress)
        if instr(config.IPAddress(i),":") = 0 then wscript.echo connectionid & space(padding) & config.IPAddress(i)
    next
next

Open in new window

0
 
crp0499CEOAuthor Commented:
Update:
I did not have a number in the first iSCSI interface once I added the number it aligned. But I would still like to know if there is a way to fix that if I do not have the number there.  It would seem from the looks of it that it aligns the interfaces with numbers in them correctly and the one without it does not. What are your thought on this?
0
 
clockwatcherCommented:
Are you talking about the new code I posted?  The new code should pad out the shorter interfaces to match the longest and then add 3 more spaces to everything.  If that's not lining up, you're probably not using a fixed-width font and could be out of luck.  You could try adding the spaces and then inserting a tab- there's a chance that may get you lined up even with a non-fixed width font.

In other words, replace line 21 and on with,
dim i, config, connectionid, padding
for each config in colSettingsNetAdapConfig
    connectionid = adapters(config.InterfaceIndex)
    padding = maxlen - len(connectionid)
    for i = lbound(config.IPAddress) to ubound(Config.IPAddress)
        if instr(config.IPAddress(i),":") = 0 then wscript.echo connectionid & space(padding) & vbTab & config.IPAddress(i)
    next
next

Open in new window

0
 
crp0499CEOAuthor Commented:
No, the previous one, but I did try the new code. I think we're on the right track, this is what it does now

Interface 2    10.10.10.10
iSCSI1     192.168.5.20
iSCSI2     192.168.10.20


I may just be picky, but what if we could take the longest string and add +3 to it for the others.  Is that possible, or am I asking too much?
0
 
clockwatcherCommented:
At this point I'm not sure which code you're working with but that's exactly what the second post should have done.  If it didn't line things up then you're not using a fixed width font to display things and no amount of messing with spaces is going to get it to line up.
0
 
crp0499CEOAuthor Commented:
Ok, you got me where I wanted. I will deal with the rest. Thank you Clockwatcher, you are the best.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now