I have the following code which I have blatantly ripped off a publicly
available script:

Set oWMIService = GetObject ("winmgmts:" & "!\\" & strComputerName &
"\root\cimv2")
Set oAdapters = oWMIService.ExecQuery ("Select * from
Win32_NetworkAdapterConfiguration Where IPEnabled=True")

For Each oAdapter in oAdapters
strIPAddress = oAdapter.IPAddress(0)
Wscript.Echo strIPAddress
Next

The For/Next loop results in the following display:

10.100.1.100
192.168.245.1
192.168.144.1

I am really only interested in obtaining the top IP address and assigning it
to the strIPAddress variable. Currently, when that script runs the
strIPAddress variable is left with the IP number of the lowest priority
adapter and I would like to have only the IP address of the highest priority
adapter.

Can someone please show me how to re-write this code to accomplish this
objective?

Thanks in advance!

Mike

Re: WMI Question... (newbie to scripting here) by Torgeir

Torgeir
Tue Sep 14 19:48:41 CDT 2004

Mike Perry wrote:

> I have the following code which I have blatantly ripped off a publicly
> available script:
>
> Set oWMIService = GetObject ("winmgmts:" & "!\\" & strComputerName &
> "\root\cimv2")
> Set oAdapters = oWMIService.ExecQuery ("Select * from
> Win32_NetworkAdapterConfiguration Where IPEnabled=True")
>
> For Each oAdapter in oAdapters
> strIPAddress = oAdapter.IPAddress(0)
> Wscript.Echo strIPAddress
> Next
>
> The For/Next loop results in the following display:
>
> 10.100.1.100
> 192.168.245.1
> 192.168.144.1
>
> I am really only interested in obtaining the top IP address and assigning it
> to the strIPAddress variable. Currently, when that script runs the
> strIPAddress variable is left with the IP number of the lowest priority
> adapter and I would like to have only the IP address of the highest priority
> adapter.
>
> Can someone please show me how to re-write this code to accomplish this
> objective?
Hi

Here is one way of doing it:


For Each oAdapter in oAdapters

strIPAddress = oAdapter.IPAddress(0)

Exit For ' exit loop after first run

Next

Wscript.Echo strIPAddress



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: WMI Question... (newbie to scripting here) by Mike

Mike
Tue Sep 14 23:13:20 CDT 2004

Torgeir,

Thanks for your input, actually I have already considered an option very
similar to that but I consider that less than clean and crisp programming.
Seems to me that there ought to be a way to do a single line statement that
would eliminate more than a couple lines of the code in my example.
Something along the lines of:

strIPAddress = oWMIService.ExecQuery(Select blah blah blah)

Where the query returns just the single highest priority IP address on the
first adapter that has IPEnabled set to True..

Programmically that isn't perhaps good programming either since there can
always be more than one adapter and the IP address you are looking for is
assigned to the second adapter, but in my case I am getting information from
each workstation in a startup script and I know that of the few systems on
my network that have more than one adapter that there is only one adapter
with an IP address assignment.

> Hi
>
> Here is one way of doing it:
>
>
> For Each oAdapter in oAdapters
>
> strIPAddress = oAdapter.IPAddress(0)
>
> Exit For ' exit loop after first run
>
> Next
>
> Wscript.Echo strIPAddress

"Torgeir Bakken (MVP)" <Torgeir.Bakken-spam@hydro.com> wrote in message
news:%23hLdL3rmEHA.3328@TK2MSFTNGP10.phx.gbl...
> Mike Perry wrote:
>
> > I have the following code which I have blatantly ripped off a publicly
> > available script:
> >
> > Set oWMIService = GetObject ("winmgmts:" & "!\\" & strComputerName &
> > "\root\cimv2")
> > Set oAdapters = oWMIService.ExecQuery ("Select * from
> > Win32_NetworkAdapterConfiguration Where IPEnabled=True")
> >
> > For Each oAdapter in oAdapters
> > strIPAddress = oAdapter.IPAddress(0)
> > Wscript.Echo strIPAddress
> > Next
> >
> > The For/Next loop results in the following display:
> >
> > 10.100.1.100
> > 192.168.245.1
> > 192.168.144.1
> >
> > I am really only interested in obtaining the top IP address and
assigning it
> > to the strIPAddress variable. Currently, when that script runs the
> > strIPAddress variable is left with the IP number of the lowest priority
> > adapter and I would like to have only the IP address of the highest
priority
> > adapter.
> >
> > Can someone please show me how to re-write this code to accomplish this
> > objective?
> Hi
>
> Here is one way of doing it:
>
>
> For Each oAdapter in oAdapters
>
> strIPAddress = oAdapter.IPAddress(0)
>
> Exit For ' exit loop after first run
>
> Next
>
> Wscript.Echo strIPAddress
>
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
> http://www.microsoft.com/technet/scriptcenter/default.mspx



Re: WMI Question... (newbie to scripting here) by Torgeir

Torgeir
Wed Sep 15 08:14:51 CDT 2004

Mike Perry wrote:

> Torgeir,
>
> Thanks for your input, actually I have already considered an option very
> similar to that but I consider that less than clean and crisp programming.
> Seems to me that there ought to be a way to do a single line statement that
> would eliminate more than a couple lines of the code in my example.
> Something along the lines of:
>
> strIPAddress = oWMIService.ExecQuery(Select blah blah blah)
>
> Where the query returns just the single highest priority IP address on the
> first adapter that has IPEnabled set to True..

As far as I know, this is not possible...


> Programmically that isn't perhaps good programming either since there can
> always be more than one adapter and the IP address you are looking for is
> assigned to the second adapter, but in my case I am getting information from
> each workstation in a startup script and I know that of the few systems on
> my network that have more than one adapter that there is only one adapter
> with an IP address assignment.


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx