News

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

Mitch Tulloch

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

    Requires Free Membership to View

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

 

 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 using Network Monitor 3.0

Part 9: Understand remote scripting

Part 10: Neat WMI tricks

Part 11: Remote scripting

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 so 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 line:

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

could be replaced by:

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

@42516

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 the statement:

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

is executed, 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_ and 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. And 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

OK so 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 the 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.