If you would like to read the other parts in this article series please go to:
- Managing Windows Networks Using Scripts – Part 1: The Basics
- Managing Windows Networks Using Scripts – Part 2: Cleaning Up
- Managing Windows Networks Using Scripts – Part 3: Understanding WMI
- Managing Windows Networks Using Scripts – Part 4: Using Win32_NetworkAdapterConfiguration
- Managing Windows Networks Using Scripts – Part 5: Getting Over the Hump
- Managing Windows Networks Using Scripts – Part 6: Remote Scripting First Steps
- Managing Windows Networks Using Scripts – Part 7: Troubleshooting the Mystery Error
- Managing Windows Networks Using Scripts – Part 8: Troubleshooting Remote Scripting using Network Monitor 3.0
- Managing Windows Networks Using Scripts – Part 9: Understanding Remote Scripting
- Managing Windows Networks Using Scripts – Part 10: Remote Scripting Tricks
- Managing Windows Networks Using Scripts – Part 11: More Scripting Tricks
- Managing Windows Networks Using Scripts – Part 12: Properties of a WMI Class
- Managing Windows Networks Using Scripts – Part 14: Learning More about WMI Scripting
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
On Error Resume Next
Dim strComputer
Dim strWMINamespace
Dim strWMIQuery
Dim objWMIService
Dim colItems
Dim objItem
strComputer = “.”
strWMINamespace = “\root\CIMV2”
strWMIQuery = “:Win32_BootConfiguration”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & strWMINamespace & strWMIQuery)
WScript.Echo “Number of properties of ” & strWMIQuery & ” class is ” & objWMIService.Properties_.count
For Each objItem in objWMIService.Properties_
Wscript.Echo “Property: ” & objItem.name
Next
Get the Windows Vista Resource Kit today!
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
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Number of properties of :Win32_BootConfiguration class is 9
Property: BootDirectory
Property: Caption
Property: ConfigurationPath
Property: Description
Property: LastDrive
Property: Name
Property: ScratchDirectory
Property: SettingID
Property: TempDirectory
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
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Number of properties of :Win32_DiskPartition class is 34
Property: Access
Property: Availability
Property: BlockSize
Property: Bootable
Property: BootPartition
Property: Caption
Property: ConfigManagerErrorCode
Property: ConfigManagerUserConfig
Property: CreationClassName
Property: Description
Property: DeviceID
Property: DiskIndex
Property: ErrorCleared
Property: ErrorDescription
Property: ErrorMethodology
Property: HiddenSectors
Property: Index
Property: InstallDate
Property: LastErrorCode
Property: Name
Property: NumberOfBlocks
Property: PNPDeviceID
Property: PowerManagementCapabilities
Property: PowerManagementSupported
Property: PrimaryPartition
Property: Purpose
Property: RewritePartition
Property: Size
Property: StartingOffset
Property: Status
Property: StatusInfo
Property: SystemCreationClassName
Property: SystemName
Property: Type
Display the Values of Each Property
Now 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
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Number of properties of :Win32_BootConfiguration class is 9
Property: BootDirectory Value:
Property: Caption Value:
Property: ConfigurationPath Value:
Property: Description Value:
Property: LastDrive Value:
Property: Name Value:
Property: ScratchDirectory Value:
Property: SettingID Value:
Property: TempDirectory Value:
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:
Figure 1: Key property for Win32_BootConfiguring 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:
Figure 2: The Windows Management Instrumentation Tester
Click the Connect button and connect to the root\cimv2 namespace:
Figure 3: Connecting to the Win32_BootConfiguration class
Click Connect to return to the main window where all the buttons now show as available:
Figure 4: Connected to the class
Now click the Enum Instances button and type the class name so you can display all the instances of the class:
Figure 5: Displaying the instances of the class
Finally, click OK to show all the instances of the class as enumerated by their key property (Name):
Figure 6: Instances of Win32_BootConfiguration
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
On Error Resume Next
Dim strComputer
Dim strWMINamespace
Dim strWMIQuery
Dim objWMIService
Dim colItems
Dim objItem
strComputer = “.”
strWMINamespace = “\root\CIMV2”
strWMIQuery = “:Win32_BootConfiguration.Name=’BootConfiguration'”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & strWMINamespace & strWMIQuery)
WScript.Echo “Number of properties of ” & strWMIQuery & ” class is ” & objWMIService.Properties_.count
For Each objItem in objWMIService.Properties_
Wscript.Echo “Property: ” & objItem.name & vbTab & “Value: ” & objItem.value
Next
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
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Number of properties of :Win32_BootConfiguration.Name=’BootConfiguration’ class
is 9
Property: BootDirectory Value: \WINDOWS
Property: Caption Value: \Device\Harddisk0\Partition1
Property: ConfigurationPath Value: \WINDOWS
Property: Description Value: \Device\Harddisk0\Partition1
Property: LastDrive Value: C:
Property: Name Value: BootConfiguration
Property: ScratchDirectory Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp
Property: SettingID Value:
Property: TempDirectory Value: C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp
Putting this info into table form makes it easier to read:
BootDirectory |
\WINDOWS |
Caption |
\Device\Harddisk0\Partition1 |
ConfigurationPath |
\WINDOWS |
Description |
\Device\Harddisk0\Partition1 |
LastDrive |
C: |
Name |
BootConfiguration |
ScratchDirectory |
C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp |
SettingID |
Value: |
TempDirectory |
C:\WINDOWS\system32\config\systemprofile\Local Settings\Temp |
Conclusion
We 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!
If you would like to read the other parts in this article series please go to:
- Managing Windows Networks Using Scripts – Part 1: The Basics
- Managing Windows Networks Using Scripts – Part 2: Cleaning Up
- Managing Windows Networks Using Scripts – Part 3: Understanding WMI
- Managing Windows Networks Using Scripts – Part 4: Using Win32_NetworkAdapterConfiguration
- Managing Windows Networks Using Scripts – Part 5: Getting Over the Hump
- Managing Windows Networks Using Scripts – Part 6: Remote Scripting First Steps
- Managing Windows Networks Using Scripts – Part 7: Troubleshooting the Mystery Error
- Managing Windows Networks Using Scripts – Part 8: Troubleshooting Remote Scripting using Network Monitor 3.0
- Managing Windows Networks Using Scripts – Part 9: Understanding Remote Scripting
- Managing Windows Networks Using Scripts – Part 10: Remote Scripting Tricks
- Managing Windows Networks Using Scripts – Part 11: More Scripting Tricks
- Managing Windows Networks Using Scripts – Part 12: Properties of a WMI Class
- Managing Windows Networks Using Scripts – Part 14: Learning More about WMI Scripting