Home All Groups Group Topic Archive Search About

store WMI property value in variable



Author
3 May 2007 2:06 PM
Mecha77
Hi,
  I made a WMI query to get the IP address of the local machine. I'd like to
know how to assign the address property to a string variable so I can
manipulate it.

Thanks

Author
3 May 2007 4:22 PM
Tom Lavedas
On May 3, 10:06 am, Mecha77 <Mech***@discussions.microsoft.com> wrote:
> Hi,
>   I made a WMI query to get the IP address of the local machine. I'd like to
> know how to assign the address property to a string variable so I can
> manipulate it.
>
> Thanks

I presume you have tried and are getting an error.  Since you haven't
chosen to show the results of your attempt(s), one is left to guess at
the issue.  A common problem is a failure to process the response
appropriately.  In this case, the variable type of the IP Address
property of the collection provided by the query is a Variant array,
with just one item in it.  This can be inferred from examples in the
Technet Script Center examples and/or determined using VBSs TypeName()
method.  The latter is a technique I often use to troubleshoot a
script like this.  In this case, it also came in handy to strip out
unwanted information from my simple general query ...

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
  if typename(objItem.IPAddress) <> "Null" then
    if objItem.IPAddress(0) <> "0.0.0.0" Then
      sIPAddress = objItem.IPAddress(0)
      Wscript.Echo "IP Address: " & sIPAddress
    end if
  end if
Next

Note that this will return the LAST non-zero IP address.  So, if there
is a prossibility for two or more network connections to be in place,
this simple approach will need to be modified.

TechNet Script Center Sample Scripts (URL all one line)
http://www.microsoft.com/downloads/details.aspx?FamilyID=b4cb2678-dafb-4e30-b2da-b8814fe2da5a

Tom Lavedas
============
http://members.cox.net/tglbatch/wsh/

AddThis Social Bookmark Button