Return-all-values script: Managing Windows networks using scripts, Part 13

One way to manage your Windows network is through scripts. The particular workhorse script described in this tip can return all values of all properties of a specific instance of a Windows Management Instrumentation (WMI) class to give you useful information about your computers.

This article originally appeared on WindowsNetworking.com.
You can manage your Windows Networks using scripts. In this tip, learn how to modify a workhorse or "return all values" script to list the names and values of any property of a Windows Management Instrumentation (WMI) class. This tip originally appeared on WindowsNetworking.com.

In the previous article of this series, Properties of Windows Management Instrumentation, 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

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 take the following line:

strWMIQuery = ":Win32_BootConfiguration"

…and change it 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

Read other 'Managing Windows networks using scripts' tips
Part 1: The basics

Part 2: Cleaning up

Part 3: Understanding WMI

Part 4: Using Win32_NetworkAdapterConfiguration

Part 5: Getting over the hump

Part 6: Remote scripting first steps

Part 7: Troubleshooting the mystery error

Part 8: Troubleshooting remote scripting with Network Monitor 3.0

Part 9: Understand remote scripting

Part 10: Two tricks using WMI scripts

Part 11: More remote scripting tips

Part 12: Properties of WMI class

Part 13: A return-all-values script

Part 14: WMI scripting resources
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 displaying 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. To 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? To see 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 follows:

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. So 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 Win32_BootConfiguration class on MSDN 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:

Key property for Win32_BootConfiguring 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:

The Windows Management Instrumentation Tester
Figure 2: The Windows Management Instrumentation Tester

Click the Connect button and connect to the root\cimv2 namespace:

Connecting to the Win32_BootConfiguration class
Figure 3: Connecting to the Win32_BootConfiguration class

Click Connect to return to the main window where all the buttons now show as available:

Connected to the class
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:

Displaying 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):

Instances of Win32_BootConfiguration
Figure 6: Instances of Win32_BootConfiguration

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!

About the author:
Mitch Tulloch is a writer, trainer and consultant specializing in Windows server operating systems, IIS administration, network troubleshooting, and security. He is the author of 15 books including the Microsoft Encyclopedia of Networking (Microsoft Press), the Microsoft Encyclopedia of Security (Microsoft Press), Windows Server Hacks (O'Reilly), Windows Server 2003 in a Nutshell (O'Reilly), Windows 2000 Administration in a Nutshell (O'Reilly), and IIS 6 Administration (Osborne/McGraw-Hill). Mitch is based in Winnipeg, Canada, and you can find more information about his books at his website: www.mtit.com.

WindowsNetworking.com contains a wealth of networking information for administrators: Featuring information on how to setup and troubleshoot various networks of any size. Also includes a comprehensive archive of hundreds of reviewed networking software and hardware solutions. Frequently updated with articles and tips by a team of leading authors, it remains a favorite within the networking community.


This was first published in November 2008

Dig deeper on Network Monitoring

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchSDN

SearchEnterpriseWAN

SearchUnifiedCommunications

SearchMobileComputing

SearchDataCenter

SearchITChannel

Close