none
Enumerating fields returned with a WMI query RRS feed

  • Question

  • There are lots of good examples out there of using VBScript to run a query and write the results to the screen.
    For each obj in col
     wscript.echo obj.property
    next

    What I want to do is enumerate all the fields which are returned and echo the property and value to the screen. In ADO you would do something like:
    for each fld in rs.fields
     wscript.echo fld.name & " = " & fld.value
    next

    I nearly lost my mind reading about ExecQuery and the objects it returns. If anyone has some sample code it would be greatly appriciated. The reason for this is to allow me to be able to modify my SCCM query without having to then modify the code which writes it out to the screen.

    Thanks!

    Rob

    --edit--
    I found the code I was experimenting with, this is the relevant bit:
    For Each qres In qresults
            set qw = qres.subclasses_
            for each qw in qobs
                    for each qprop in qw.properties_
                            wscript.echo qprop.name & "=" & qprop.value
                    next
            next

    What I get out is a whole bunch of:
    SMS_G_System_COMPUTER_SYSTEM =
    (one for every field being returned).

    Someone please help!
    Sunday, October 4, 2009 6:42 PM

Answers

  • sHost = "."
    sNamespace = "\root\cimv2"
    sQuery = "SELECT * FROM Win32_Process"
    
    Set oSWbemServices = GetObject("winmgmts:\\" & sHost & sNamespace)
    set processcollection = oSWbemServices.execquery(squery)
    
    for each process in processcollection
    	For Each property In process.properties_
    		wscript.echo property.name & "=" & property.value
    	Next
    Next
    Something like this?
    or do you want to add another step?
    This is enumerating all properties for all instances from a given class?
    I get a feeling you want this, but then for all classes in a given namespace?
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Tuesday, November 10, 2009 3:40 PM
    Moderator

All replies

  • What are you trying to do EXACTLY?
    Monday, October 5, 2009 7:51 AM
    Moderator
  • A PHP site on a debian box needs this info, so it's going to connect to IIS on a 2K3 box and pass a WMI query. The script will run the query and send the results back as XML. The PHP script will then do whatever it pleases with it. If I decide I want a new field, or to drop a field, or something, I only want to change it on the PHP side.

    Monday, October 5, 2009 11:35 AM
  • IDontWanna,

    While this blurs the line between SCCM and Scripting forums - let's give it a shot...

    1.  The Script Center is your friend:
    -http://technet.microsoft.com/en-us/scriptcenter/default.aspx
    -You may have already come across this site - but it has example upon example of script to pull specific data from WMI.  If you are looking for a script to return disk drives?  It's already there.  Pull back MAC addresses from active NIC's?  There's a script for that too.

    2.  Enumerating Objects:
    Below is an example script on connecting to a namespace and returning objects.  You can do the same thing with SCCM WMI class/instances.  It really depends on what you are querying on how the script would look -but the item below should get you on your way.  You just need to connect to your server's sccm namespaces (\root\sms\<site>) and then create your query.

    Option Explicit

    Dim oSWbemServices

    Dim sHost

    Dim sNamespace

    Dim sQuery

    Dim oProcessColl

    Dim oProcess

     

    sHost = "."

    sNamespace = "\root\cimv2"

    sQuery = "SELECT Name, ProcessID FROM Win32_Process WHERE Name = 'notepad.exe'"

    Set oSWbemServices = GetObject("winmgmts:\\" & sHost & sNamespace)

    Set oProcessColl= oSWbemServices.ExecQuery(sQuery)

    For Each oProcess In oProcessColl

        WScript.Echo "Terminating " & oProcess.Name

        WScript.Echo "ProcessID: " & oProcess.ProcessID

        oProcess.Terminate

    Next



    3.  WMI Administrator Tools
    -http://www.microsoft.com/downloads/details.aspx?familyid=6430F853-1120-48DB-8CC5-F2ABDC3ED314&displaylang=en
    -This is a great way to connect to objects and test queries to see what the results returned are (CIM Studio).  A ton better than wbemtest.

    4.  The WMI SDK
    -http://msdn.microsoft.com/en-us/library/aa390386(VS.85).aspx
    -one more enumeration example - and also has information on synchronous or asynchronous querying depending on your need.
    -This variant of object enumeration is built on connecting to the instance directly rather than running a query.  Again, it just depends on what you are querying for.

    If this doesn't help - as Torsten mentioned - ask EXACTLY the items you want returned:
    Ex:  "I want to query WMI to return all ConfigMgr Clients that are Windows Vista OS and loop through it to see all computer names and users"
    Tuesday, October 6, 2009 3:51 AM
  • Could you post the entire script, I think I understand what you are trying to do, but part of the script you are using is missing.
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Tuesday, November 10, 2009 2:59 PM
    Moderator
  • sHost = "."
    sNamespace = "\root\cimv2"
    sQuery = "SELECT * FROM Win32_Process"
    
    Set oSWbemServices = GetObject("winmgmts:\\" & sHost & sNamespace)
    set processcollection = oSWbemServices.execquery(squery)
    
    for each process in processcollection
    	For Each property In process.properties_
    		wscript.echo property.name & "=" & property.value
    	Next
    Next
    Something like this?
    or do you want to add another step?
    This is enumerating all properties for all instances from a given class?
    I get a feeling you want this, but then for all classes in a given namespace?
    "Everyone is an expert at something" Kim Oppalfens Configmgr expert for lack of any other expertise. http://www.scug.be/blogs/sccm
    Tuesday, November 10, 2009 3:40 PM
    Moderator