Properties of Windows Management Instrumentation: Managing Windows networks using scripts, Part 12

Learn how to display all properties of any Windows Management Instrumentation (WMI) class in this tip on how to manage Windows networks using scripts.

This article originally appeared on WindowsNetworking.com.
Learn how to display all properties of any Windows Management Instrumentation (WMI) class in this tip on how to manage Windows networks using scripts, originally posted on WindowsNetworking.com.

Back in Understanding WMI, the third article in this series, we developed the following simple script named displayTimeZone.vbs that displays the current time zone setting on your machine:

Option Explicit
On Error Resume Next
Dim strComputer
Dim strWMINamespace
Dim strWMIQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
strWMINamespace = "\root\CIMV2"
strWMIQuery = "SELECT * FROM Win32_TimeZone"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace)
Set colItems = objWMIService.ExecQuery(strWMIQuery)

For Each objItem In colItems
            WScript.Echo objItem.Caption
Next

When I run this script, I get the following result:

C:\scripts>DisplayTimeZone.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

(GMT-06:00) Central Time (US & Canada)

How did I know that it was the Caption property of the Win32_TimeZone class that contains the information I want to display? By reading the description of the Win32_TimeZone WMI class on MSDN. In fact, this MSDN page tells us that the Description property basically returns the same information as the Caption property, so I could have changed the line WScript.Echo objItem.Caption to WScript.Echo objItem.Description and gotten the same result.

What else does this MSDN page tell us about the Win32_TimeZone class? Well, what if I wanted to find out what month daylight savings time goes into effect on our machine? A quick reading of the page leads me to this information concerning the DaylightMonth property:

DaylightMonth
Data type: uint32
Access type: Read-only

Month when the transition from standard time to daylight saving time occurs on an operating system.

Value Meaning
1
0x1 January
2
0x2 February
3
0x3 March
4
0x4 April
5
0x5 May
6
0x6 June
7
0x7 July
8
0x8 August
9
0x9 September
10
0xA October
11
0xB November
12
0xC December

To use this info, I simply change the line WScript.Echo objItem.Caption to WScript.Echo objItem.DaylightMonth and here's what I get when I run my script:

C:\scripts>DisplayTimeZone.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

3

Sure enough, daylight savings kicked in in March this year instead of April because of the changes legislated by the US government (and copied here in Canada).

Enumerating the properties of a class

Now we could continue our learning process by changing the <property> in WScript.Echo objItem.<property>, and gradually work through displaying each of the properties of the Win32_TimeZone class one by one, but could there be an easier way? Is it possible to display all the properties of this class in a single script without having to actually name them in the script? Sure! But before we do this, let's first try and enumerate the number of properties of this class. Here's how we do 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_TimeZone"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)
WScript.Echo "Number of properties of " & strWMIQuery & " class is " & objWMIService.Properties_.count

Here's the result of running this new script which we'll call DisplayClassProperties.vbs:

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_TimeZone class is 24

A quick tallying of properties on the Win32_TimeZone MSDN page tells us this is correct i.e. the Win32_TimeZone class has 24 properties in total.

How does this new script work? Well, first you'll notice that instead of connecting to the default namespace ("\root\CIMV2") on the local computer (".") we're connecting directly to the Win32_TimeZone class on the computer. In other words, the following line:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)

...could be replaced by this:

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_TimeZone")

...and it would do the same thing. The other thing you'll notice is this funny-looking thing in the last line of the script:

objWMIService.Properties_.count

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

That's weird, isn't it? We know about properties (e.g. <object>.<property>) and methods (e.g. <object>.<method>) from The basics of managing Windows networks using scripts, the first article of this series, but objWMIService.Properties_.count has two periods in it not one. What's happening here? Well, let's go back to this line again:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery)

...which we saw is equivalent to this:

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_TimeZone")

The moniker in this WMI statement is winmgmts:\\.\root\CIMV2:Win32_TimeZone and it defines the path to the WMI class we're interested in gaining access to, and when you use the GetObject function on this moniker, it returns an SWbemObject object which is then assigned to the objWMIService variable using the Set statement. (More accurately, the GetObject function returns a reference to an SWbemObject object provided by a COM component, but who needs to sound that technical unless you're trying to impress someone.)

So in other words, when this statement is executed:

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2:Win32_TimeZone")

...we get an SWmebObject object returned back to us. Now objects have properties, so what properties can an SWmebObject object have? Well, one property it can have is called Properties_; using the standard <object><properties> notation this means that the Properties_ property of an SWebmObject object would be denoted by SWbemObject.Properties_ (yes the underscore is part of the property name). You can read more about the SWbemObject.Properties_ property of the SWebmObject object on MSDN, and this tells us that this property is "a collection of the properties for the current class or instance" or in other words, the SWbemObject.Properties_ property is a collection. And remember that a collection is a type of object that contains multiple elements (which can be either other objects or properties but not methods).

So in other words, SWbemObject.Properties_ is actually a collection (a type of object) and since objects can have properties, so can this object. One property that a collection can have is .count which returns the number of properties of the object, i.e., <collection>.<count> returns the number of elements in the collection. So this means that SWbemObject.Properties_.count is the .count property of the SWbemObject.Properties_ object, hence the two periods instead of the usual single period in the <object>.<property> syntax.

At least, that's how I understand this thing, but please remember I'm not a developer -- I'm just a geek like yourself who is learning how to script so he can do his job!

Displaying the properties of a class

Now that we can enumerate the (return the number of) properties of the Win32_TimeZone class, how do we return the names of the actual properties themselves? By appending the following lines to the end of our DisplayClassProperties.vbs script:

For Each objItem in objWMIService.Properties_
    Wscript.Echo "Property: " & objItem.name
Next

Here's what we get when we now run our 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_TimeZone class is 24
Property: Bias
Property: Caption
Property: DaylightBias
Property: DaylightDay
Property: DaylightDayOfWeek
Property: DaylightHour
Property: DaylightMillisecond
Property: DaylightMinute
Property: DaylightMonth
Property: DaylightName
Property: DaylightSecond
Property: DaylightYear
Property: Description
Property: SettingID
Property: StandardBias
Property: StandardDay
Property: StandardDayOfWeek
Property: StandardHour
Property: StandardMillisecond
Property: StandardMinute
Property: StandardMonth
Property: StandardName
Property: StandardSecond
Property: StandardYear

Checking our previous page on MSDN describing the Win32_TimeZone class, we can see from the above that the names of all the properties of the class have been successfully displayed.

Conclusion
The power of this approach is that it allows us to list the names of the properties of any WMI class so we can learn more about it. (Remember, in Understanding WMI, the third article in this series, we saw how to list all the WMI classes of a namespace, and armed with that list we can go exploring and see what we can manage using WMI.)

For example, say we wanted to list the properties of the Win32_BootConfiguration WMI class. To do this using our DisplayClassProperties.vbs script, all we need to do is change this line:

strWMIQuery = ":Win32_TimeZone"

...to read as follows:

strWMIQuery = ":Win32_BootConfiguration"

When we make this change and re-run our script, we get this 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  Value:
Property: ScratchDirectory    
Property: SettingID   
Property: TempDirectory

I encourage you to use this present article together with the third article in this series, Understanding WMI, to explore WMI classes and their properties further on your own, and we'll see more about the power of using WMI to manage Windows computers in future articles in this series.

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 October 2008

Dig deeper on Working With Servers and Desktops

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