| Practical Scripting Part 13: A Handy Return-All-Values Script |
|
|
|
|
In the previous article of this series we came up with a script called DisplayClassProperties.vbs that displays the names of all the properties of a WMI class. Here’s what this script looks like, using Win32_BootConfiguration as the class we’re connecting to in our WMI moniker: Option Explicit strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) For Each objItem in objWMIService.Properties_ When I run this script (using local admin credentials!) on my Windows XP workstation (with Cscript.exe pre-configured as the default Windows script host) I get the following result: C:\scripts>DisplayClassProperties.vbs Number of properties of :Win32_BootConfiguration class is 9 I also mentioned in the last article that this script could easily be customized to display the names of properties of any WMI class. For example, say we wanted to display all the names of all the properties in the Win32_DiskPartition class. To do this, all we need to do is change the line: strWMIQuery = ":Win32_BootConfiguration" to this: strWMIQuery = ":Win32_DiskPartition" Now when we run our script again, we get the following result: C:\scripts>DisplayClassProperties.vbs Number of properties of :Win32_DiskPartition class is 34 Display the Values of Each PropertyNow at this point you may be saying, So what? All this script does is display the name of each property of a class—what about display the value of each property? Well that’s a good point! Let’s see if we can modify the script (we’re back to using Win32_BootConfiguration as our class) so that the script will list not only the names of all the properties but also their values. Do do this, let’s try changing this line: Wscript.Echo "Property: " & objItem.name to this: Wscript.Echo "Property: " & objItem.name & vbTab & "Value: " & objItem.value Here's what we get when we now run the script: C:\scripts>DisplayClassProperties.vbs Number of properties of :Win32_BootConfiguration class is 9 Hmm, all the values are null i.e. blank. Why? Well, here's what's going on. Look at this line: Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) Plugging in the values of each variable, we could rewrite this line as this: Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_BootConfiguration") Note that we're connecting to a specific WMI class (Win32_BootConfiguration) in our WMI moniker string so that we can return a collection containing all properties of this class. Then we want to display the name and value of each property. But the values are returned as NULL because we haven't connected to a specific instance of this class. The WMI Glossary says that an instance is "a representation of a real-world managed object that belongs to a specific class" and that "instances contain actual data" and actual data is what we want, right? Well, how do we connect to an instance of a class? To connect to an instance of a class, you need to specify a particular instance by using the key property of the class. Again consulting the WMI Glossary, we see that the key property is "a property that provides a unique identifier for an instance of a class" and that "key properties are marked with the Key qualifier" in MSDN documentation. Let's look at the MSDN page for the Win32_BootConfiguration class to learn what the key property is for this class. Figure 1 shows the portion of this page that identifies the key property for this class:
From this MSDN page we can see that the key property for the Win32_BootConfiguration class is Name. This means that we need to specify a value for this property in our WMI moniker string if we want to connect to a specific instance of this class to retrieve the values of each property for the class. In other words, all we need to do is change this line: strWMIQuery = ":Win32_BootConfiguration" to this: strWMIQuery = ":Win32_BootConfiguration.Name='SOMETHING'" where 'SOMETHING' is the value of the Name property of specific instance of the class. So how can we find out the value of the key property of a specific instance of this class? One way is to use the Windows Management Instrumentation Tester (wbemtest.exe). Start by typing wbemtest at a command prompt. This opens the following window:
Click the Connect button and connect to the root\cimv2 namespace:
Click Connect to return to the main window where all the buttons now show as available:
Now click the Enum Instances button and type the class name so you can display all the instances of the class:
Finally, click OK to show all the instances of the class as enumerated by their key property (Name):
Well, after all that it turns out that there is only one instance of this class on our machine and the Name property of this instance has the value "BootConfiguration"! So this means that in order to display the values of all properties of the instance of the Win32_BootConfiguration class on our machine, all we need to do is change this line: strWMIQuery = ":Win32_BootConfiguration" to this: strWMIQuery = ":Win32_BootConfiguration.Name='BootConfiguration'" In other words, our revised DisplayClassProperties.vbs script now looks like this: Option Explicit strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) For Each objItem in objWMIService.Properties_ And now, when we run this script it displays not just the names of all the properties but also their values: C:\scripts>DisplayClassProperties.vbs Number of properties of :Win32_BootConfiguration.Name='BootConfiguration' class Putting this info into table form makes it easier to read:
ConclusionWe can see that this simple "return-all-values" script has given us some useful information about our machine! Now here's an exercise you can try on your own: instead of connecting an instance of the Win32_BootConfiguration class (there's only one instance of this class), try connecting to an instance of the Win32_DiskPartition class (which has several instances if your machine has more than one partition). To do this, first use wbemtest to display the instances of this class (and to learn the key property that distinguishes these instances) and then modify the DisplayClassProperties.vbs script so it displays the properties and values of the specified instance of this class (i.e. of the disk partition you specify). Good luck! |
|||||||||||||||||||
| < Prev | Next > |
|---|









