| Practical Scripting Part 12: Properties of a WMI Class |
|
|
|
|
Back in 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 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace) For Each objItem In colItems When I run this script, I get the following result: C:\scripts>DisplayTimeZone.vbs (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 Month when the transition from standard time to daylight saving time occurs on an operating system. Value Meaning 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 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 ClassNow 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 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & strWMINamespace & strWMIQuery) Here's the result of running this new script which we'll call DisplayClassProperties.vbs: C:\scripts>DisplayClassProperties.vbs 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 That's weird, isn't it? We know about properties (e.g. <object>.<property>) and methods (e.g. <object>.<method>) from 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 this page 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 ClassOK 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_ Here's what we get when we now run our script: C:\scripts>DisplayClassProperties.vbs Number of properties of :Win32_TimeZone class is 24 Checking our previous MSDN page 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. ConclusionThe 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 the third article of 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 Number of properties of :Win32_BootConfiguration class is 9 I encourage you to use this present article together with the third one in this series 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. |
| < Prev | Next > |
|---|








