Searching with WQL
So far I've shown how to instantiate specific objects, such as a
logical drive, and how to enumerate all the instances of a particular
class using the
Knowing how to do both of these functions will take us a long way with
WMI, but we are missing one other important capability: the ability to
find objects that meet certain criteria.
The creators of WMI found an elegant way to handle this problem. They implemented a subset of the Structured Query Language (SQL) known as the WMI Query Language (WQL). WQL greatly increases the power of WMI by giving the programmer a wide range of flexibility in locating objects. Unfortunately, WQL only supports read-only operations. You can not modify, update, or delete values in the WMI repository with WQL.
With WQL, we can even perform the same function as the
InstancesOf method we used earlier. The
following query retrieves all the
Win32_LogicalDisk objects on a system:
select * from Win32_LogicalDisk
We can use any property available on
Win32_LogicalDisk objects as criteria in our
search. As an example, let's say we wanted to find all NTFS logical
disks that have less than 100 MB of available space. The query would
look like the following:
select * from Win32_LogicalDisk where FreeSpace < 104857600 and filesystem = 'NTFS'
Pretty easy, huh? Now let's put WQL to use. First we need to get a
WMI object to the namespace we want to query. After we've done that, we
can call the
ExecQuery method on that object and pass ...