Solved

Formatting of WMI Object Information

Posted on 2004-09-29
6
386 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
ID: 12186018
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
ID: 12186306
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
ID: 12186408
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:Grime121
ID: 12186651
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
ID: 12186666
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
ID: 12188641
Thanks for the information.  I'll take a look a the script.  Sounds like a great tool.  Glad I could help.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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