Solved

Formatting of WMI Object Information

Posted on 2004-09-29
6
381 Views
Last Modified: 2008-02-01
I don't know a whole lot about VBS, but enough to mess it up I guess. This script originally put all of the hardware information for a computer into a text file and wrote each piece of information to a new line with nice spacing and everything. I needed the info that we wanted to have to be all on one line seperated by commas. I've got most of it working how i want. The only problem I'm having now is I can't stop it from giving me certain information that I do not need. Here are the parts of the script that are in question:

'Physical memory information
strProperties = "MaxCapacity"
QueryInstances "Win32_PhysicalMemoryArray",strProperties,"None"

'Logical Disk information
strProperties = _
  "DriveType,Description"
QueryInstances "Win32_LogicalDisk",strProperties,"DriveType!=3 AND DriveType !=4"

'Disk drive information
strProperties = "Model,Size"
QueryInstances "Win32_DiskDrive",strProperties,"None"

'Processor information
strProperties = "Manufacturer,MaxClockSpeed"
QueryInstances "Win32_Processor",strProperties,"None"

'NIC information
strProperties = "MACAddress,ProductName"
QueryInstances "Win32_NetworkAdapter",strProperties,_
  "AdapterType='Ethernet 802.3' AND ProductName !='Packet Scheduler Miniport'"

'Video adapter information
strProperties = "Name"
QueryInstances "Win32_VideoController",strProperties,"None"  

'BIOS information
strProperties = "Manufacturer,Version"
QueryInstances "Win32_BIOS",strProperties,"None"


'**************************************************
Sub QueryInstances(objClass,Properties,Conditions)
  If Conditions = "None" Then
    strSelect = "Select " & Properties & " From " & objClass
  Else
    strSelect = "Select " & Properties & " From " & objClass & _
      " Where " & Conditions
  End If
  Set objClassName = _
    objWMIService.ExecQuery _
      (strSelect,,wbemFlagReturnImmediately + wbemFlagForwardOnly)
  For Each objComponent in objClassName
    intCount = 1
    objFile.Write(",")
    For Each objProperty in objComponent.Properties_
      If intCount = 0 Then
        objFile.Write(",")
      End if
      If intCount = 1 Then
        intCount = 0
      End if    
      If ISNull(objProperty.Value) Then
        objFile.Write("NULL")
      Else
        If objProperty.CIMType <> wbemCimtypeUint32 And _
          objProperty.CIMType <> wbemCimtypeUint64 And _
          objProperty.CIMType <> wbemCimtypeSint64 Then
            objFile.Write(objProperty.Value & _
              GetUnits(objClass,objProperty.Name))
        Else
          strUnits = GetUnits(objClass,objProperty.Name)
          intValue = _
            SizeFormat(objProperty.Name,objProperty.Value,strUnits)
          objFile.Write(intValue)
        End If
      End If
    Next
  Next
End Sub


Here is a sample of the output I'm getting:

HELPDESK1,Microsoft Windows XP Professional,5.1.2600,OptiPlex GX270               ,HELPDESK1,509MB,4GB,Physical Memory Array 0,3 1/2 Inch Floppy Drive,A:,2 ,Removable Disk,D:,2 ,CD-ROM Disc,E:,5 ,CD-ROM Disc,F:,5 ,\\.\PHYSICALDRIVE1,IOMEGA ZIP 250,NULL,\\.\PHYSICALDRIVE0,Maxtor 6E040L0,38.3GB,CPU0,GenuineIntel,2261 megahertz,8,00:0B:DB:77:FD:01,Intel(R) PRO/1000 MT Network Connection,VideoController1,Intel(R) 82865G Graphics Controller,Dell Computer Corporation,Phoenix ROM BIOS PLUS Version 1.10 A02,Phoenix ROM BIOS PLUS Version 1.10 A02,3,0,DELL   - 7


I don't want these parts to be in there:

Physical Memory Array
\\.\PHYSICALDRIVE1 and \\.\PHYSICALDRIVE0 as well as the drive numbers for both logical disks and physical disks
CPU0
VideoController1
and Phoenix ROM BIOS PLUS Version 1.10 A02,3,0,DELL   - 7 (I want the first instance of the Phoenix ROM BIOS PLUS Version 1.10 A02 though)
0
Comment
Question by:Grime121
  • 3
  • 3
6 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
I played around with WMI and it appears that each query returns at least one additional property beyond what you asked for.  

1.  Win32_PhysicalMemoryArray returns a Tag property.
2.  Win32_LogicalDisk returns DeviceID
3.  Win32_DiskDrive returns DeviceID
4.  Win32_Processor returns DeviceID
5.  Win32_NetworkAdapter returns DeviceID
6.  Win32_VideoController returns DeviceID
7.  Win32_BIOS returns 3 fields that weren't asked for.

I think the way to get around this is to test the returned property names and only write to the file if the property name is one that you asked for.
0
 
LVL 1

Author Comment

by:Grime121
Comment Utility
Could you help me out with that? I know the names of the ones I want and don't want, but I'm not sure how to code a statement like that. I really don't know much about VBS. I'd imagine it would either need to replace or be included in this statement: 'For Each objComponent in objClassName'...., and would most likely be an IF statement, but I'm not sure how to code it. It would probably be easier to test for the one's I don't want an exclude them because there are a lot more that I want than I don't want.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
Sure.  Based on your code the test needs to come immediately after "For Each objProperty in objComponent.Properties_". I've redone your code below to include the test.  The test

    If InStr(1, Properties, objProperty.Name, vbTextCompare) Then

is quite simple.  You've already passed a text string, Properties, containing the names of the properties you want to QueryInstances.  The test checks to see if the property name we're dealing with now is contained in that string.  If it is, then we process the rest of the commands.  Otherwise, we ignore that property and move along.  I tested this as best I could since I don't have ll of your code.  It worked fine.  Yes, you would think it's easier to test for what we don't want sicne that list would be shorter.  But in reality that'd require us to know the names of the properties each object will return that we don't want.  It's far easier to compare the name of each property to the names of the properties we do want since we already know those names.

This should do it.  Let me know if you run into a problem or if there's anything more I can do.


Sub QueryInstances(objClass, Properties, Conditions)
    If Conditions = "None" Then
        strSelect = "Select " & Properties & " From " & objClass
    Else
        strSelect = "Select " & Properties & " From " & objClass & _
            " Where " & Conditions
    End If
    Set objClassName = _
        objWMIService.ExecQuery _
        (strSelect, , wbemFlagReturnImmediately + wbemFlagForwardOnly)
    For Each objComponent In objClassName
        intCount = 1
        objFile.Write (",")
        For Each objProperty In objComponent.Properties_
            If InStr(1, Properties, objProperty.Name, vbTextCompare) Then
                If intCount = 0 Then
                    objFile.Write (",")
                End If
                If intCount = 1 Then
                    intCount = 0
                End If
                If IsNull(objProperty.Value) Then
                    objFile.Write ("NULL")
                Else
                    If objProperty.CIMType <> wbemCimtypeUint32 And _
                        objProperty.CIMType <> wbemCimtypeUint64 And _
                        objProperty.CIMType <> wbemCimtypeSint64 Then
                        objFile.Write (objProperty.Value & _
                        GetUnits(objClass, objProperty.Name))
                    Else
                        strUnits = GetUnits(objClass, objProperty.Name)
                        intValue = _
                        SizeFormat(objProperty.Name, objProperty.Value, strUnits)
                        objFile.Write (intValue)
                    End If
                End If
            End If
        Next
    Next
End Sub
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Grime121
Comment Utility
There we go. Works perfect now. I think I somehow lost that line from the first version of my code to the last version because now that I think about it I remember there was another For loop inside of the first For loop. Thanks a lot, man. A+ :)
0
 
LVL 1

Author Comment

by:Grime121
Comment Utility
btw, you can get the script that I based mine off of from http://www.winnetmag.com/Windows/Articles/ArticleID/39111/pg/2/2.html . There is another script that reads all of the inventory text files created by all of the computers and puts the info into an excel spreadsheet. We're going to be using the script that I modified to automate the inventory of hundreds of computers. The script will run every so often on the machines, and then be merged into an sql database that we can query through a php webpage. Very cool stuff.
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Thanks for the information.  I'll take a look a the script.  Sounds like a great tool.  Glad I could help.
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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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