Introduction to WQL: SQL for WMI - Data Queries
(Page 2 of 4 )
Data Queries
These are the most common type of WMI query. Data queries poll WMI for specific information by returning property values. These are generally also the simplest type of queries.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NTLogEvent" _
& " WHERE Logfile = 'Application'")
This is a relatively simple query. We begin by connecting to WMI’s CIMV2 Providers. Then, we make use of the WMIService’s ExecQuery method to issue the query whose results are returned as a collection of objects. The query, which is passed as a string, looks like the following:
SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Application'
We want to SELECT all (*) items from the Win32_NTLogEvent class WHERE the Logfile property is equal to “Application.”
All queries begin with the SELECT keyword. The next piece of the query determines how much information is returned about each object. The wildcard indicates that all available information should be returned.
Next is the FROM keyword. This tells WMI which of the Provider’s classes should be polled. In our example, the Win32NTLogEvent class houses all of the event log entries.
This example also makes use of a WHERE clause to further control what information is returned. The query will only return items from our class whose Logfile property matches the string “Application.” Notice that strings use single quotes, and that this is a strict match. Items “containing” this string will not be returned.
To perform a partial match in WQL, make use of the LIKE keyword in place of the = operator.
A WHERE clause must be in the form of a conditional statement. Only those items that evaluate to True will be returned. Executing this query will return a collection of objects representing each entry in the Windows NT Application Event Log.
Next: Event Queries >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer