Solved

Help with Nested For Next in a VBScript

Posted on 2013-06-02
10
789 Views
Last Modified: 2013-06-02
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
Comment
Question by:crp0499
  • 4
  • 4
  • 2
10 Comments
 
LVL 15

Expert Comment

by:ZabagaR
Comment Utility
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
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
Comment Utility
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
 
LVL 15

Expert Comment

by:ZabagaR
Comment Utility
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
 

Author Comment

by:crp0499
Comment Utility
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
 
LVL 25

Assisted Solution

by:clockwatcher
clockwatcher earned 500 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:crp0499
Comment Utility
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
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
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
 

Author Comment

by:crp0499
Comment Utility
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
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
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
 

Author Closing Comment

by:crp0499
Comment Utility
Ok, you got me where I wanted. I will deal with the rest. Thank you Clockwatcher, you are the best.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now