A favorite feature of the Query Builder is that it
automatically creates the joins for you. As an example, say you want a
list of all computers and the total amount of physical memory. To make
things a little more complex, your boss also wants this listing to
display the Active Directory Site Name, as well as a date/time stamp of
when the ConfigMgr client inventoried the information. Using the Query
Builder, you simply select the classes and properties you want to
display (this example will use the classes of System Resource, Memory,
and Workstation Status) and then run the report. The Query Builder
creates all the joins for you. To see the detail, simply click the Show
Query Language button after you write your query. To using the Query Builder, you can write the query statements
yourself. Using WQL, you specify the WMI object classes and attributes
the query will use to search the ConfigMgr site database. As an example,
perhaps you want to display all systems that have reported hardware
inventory in the past 30 days. There is no native way to do this using
the Query Builder. Luckily, ConfigMgr supports Extended WMI Query
Language, which allows you to use the following functions:
GetDate()—
This function returns the current date and time on the system. The data
is returned in date-time format (for example, 12:56 AM 12/02/2009).
DateDiff()—
This function returns the difference between two date-time values in
the increment you specify (for example, minute, hour, day). The
DateParts listed in Table 1 are supported when using DateDiff in a WHERE clause in SMS WQL. Here’s the proper syntax for DateDiff:
DateDiff ( DatePart, StartDate, EndDate )
DatePart is the part of the date you want to calculate (minute, day, month, and so on).
StartDate is the begin date.
EndDate is the ending date.
Table 1. DateParts and Abbreviations
DatePart | Abbreviation |
---|
Year | yy |
Month | mm |
Day | dd |
Hour | hh |
Minute | mi |
Second | ss |
DateAdd()—
Returns a new date-time value based on adding an interval to the
specified date. Testing validated that the DateParts listed in Table 1 are supported when using DateAdd in a WHERE clause in SMS WQL. The proper syntax for DateAdd follows:
DateAdd ( DatePart, Number, Date )
DatePart is the part of the date you want to calculate (minute, day, month, and so on).
Number is the value to increment DatePart.
Date is a valid date-time value used to calculate the new date.
Table 1 shows the DataParts and their abbreviations.
As
you can see, ConfigMgr provides a considerable amount of power in
configuring queries. If you plan to create a custom query-based
collection, first try to create a query using the Query Builder. Then
take the next step, if necessary, to incorporate your custom query
details. The next sections provide examples of using Extended WQL.
Example: Querying for Systems with a Hardware Scan in the Last 30 Days
Use this query to retrieve all systems that have reported a LastHardwareScan date within the last 30 days:
SELECT SMS_R_System.ResourceID,SMS_R_System.ResourceType,
SMS_R_System.Name,SMS_R_System.SMSUniqueIdentifier,
SMS_R_System.ResourceDomainORWorkgroup,SMS_R_System.Client
FROM from SMS_R_System inner join
SMS_G_System_WORKSTATION_STATUS on
SMS_G_System_WORKSTATION_STATUS.ResourceID = SMS_R_System.ResourceId
WHERE DATEDIFF(dd,SMS_G_System_WORKSTATION_STATUS.LastHardwareScan,GetDate()) < 30
Notice the WHERE clause in the WQL statement, which is using the DateDiff function. The query uses dd to specify the difference in days, then the LastHardwareScan property is compared to the current date using GetDate(). If the difference in days is less than 30, the system is included in the query.
Example: Querying for Systems Discovered Since Midnight
The following example includes all systems discovered since midnight, based on the CreationDate property in the discovery record.
SELECT SMS_R_System.ResourceID,SMS_R_System.ResourceType,
SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier,
SMS_R_System.ResourceDomainORWorkgroup,SMS_R_System.Client
FROM SMS_R_System
WHERE (DateDiff(day, CreationDate, GetDate()) < 1)
Now that you understand
how to create queries using the Query Builder, you may also want to
create these queries in ConfigMgr web reports. If you have built any web
reports, you probably know that there is no query builder type of tool.
It is not well known, but you can easily translate your WQL queries to
SQL. As an example, look at the query created in Figure 1, which shows you the system name, AD site, total physical memory, and last hardware scan. The WQL looks like this:
select SMS_R_System.Name, SMS_R_System.ADSiteName,
SMS_G_System_X86_PC_MEMORY.TotalPhysicalMemory,
SMS_G_System_WORKSTATION_STATUS.LastHardwareScan from SMS_R_System inner join
SMS_G_System_X86_PC_MEMORY on
SMS_G_System_X86_PC_MEMORY.ResourceId = SMS_R_System.ResourceId
inner join SMS_G_System_WORKSTATION_STATUS on
SMS_G_System_WORKSTATION_STATUS.ResourceId = SMS_R_System.ResourceId
To convert this WQL to
SQL, simply execute the query. Next, review the smsprov.log on the site
server, looking for a line that contains Execute SQL =. For this example, you would find a log entry similar to this:
[1398][Thu 03/26/2009 06:00:24]:Execute SQL =select
all SMS_R_System.Name0,SMS_R_System.AD_Site_Name0,
SMS_G_System_X86_PC_MEMORY.TotalPhysicalMemory0,
System_WORKSTATION_STATUS0.LastHWScan from System_DISC
AS SMS_R_System INNER JOIN PC_Memory_DATA AS SMS_G_System_X86_PC_MEMORY ON
SMS_G_System_X86_PC_MEMORY.MachineID = SMS_R_System.ItemKey INNER JOIN
WorkstationStatus_DATA AS System_WORKSTATION_STATUS0 ON
System_WORKSTATION_STATUS0.MachineID = SMS_R_System.ItemKey