Solved

Help with Nested For Next in a VBScript

Posted on 2013-06-02
10
798 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
ID: 39214785
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
ID: 39214796
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
ID: 39214810
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:crp0499
ID: 39214840
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
ID: 39214853
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
 

Author Comment

by:crp0499
ID: 39214860
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
ID: 39214873
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
ID: 39214886
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
ID: 39214894
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
ID: 39214908
Ok, you got me where I wanted. I will deal with the rest. Thank you Clockwatcher, you are the best.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

829 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