Returning Selected Properties of All Instances of a Class
Microsoft® Windows® 2000 Scripting Guide
As noted in the preceding section, you can create a script that runs faster and returns less extraneous data (that is, data that is not needed by the script) by using a query that requests only specific properties from a class. To do this, you replace the asterisk in a standard WQL query with the names of the properties to be returned. For example, this query returns only the Name property for all the instances of the Win32_Service class:
SELECT Name FROM Win32_Service"
If you want to return multiple properties, separate the property names with commas. This query returns the Name and State properties from Win32_Service:
SELECT Name, State FROM Win32_Service"
What does it mean to return only selected properties? Consider this script, which requests only the Name property from Win32_Service but then attempts to echo the value of the State property as well:
strComputer = "." Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colServices = objSWbemServices.ExecQuery("SELECT Name FROM Win32_Service") For Each objService In colServices Wscript.Echo objService.Name, objService.State Next
If you try to run this script under CScript, the following error message will appear in the command window:
Microsoft VBScript runtime error: Object doesn't support this property or method: 'objService.State'
Why? State is indeed a valid property of the Win32_Service class, but you did not ask for it. Therefore, State is not included within the collection of services and properties returned to you. The script acts against the returned collection, not against the Win32_Service class.
There is one exception to this: The key property for a class is always returned, even if it is not specified in the WQL query. For example, Name is the key property for the Win32_Service class. The following script retrieves only the State property from the Win32_Service class, but then attempts to display Name as well as State.
strComputer = "." Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colServices = objSWbemServices.ExecQuery("SELECT State FROM Win32_Service") For Each objService In colServices Wscript.Echo objService.Name, objService.State Next
If you run this script under Cscript, it will not fail. Instead, output similar to the following appears in the command window:
Alerter Stopped ALG Stopped AppMgmt Stopped Ati HotKey Poller Stopped AudioSrv Running BITS Running