Joining two tables in WQL/SCCM
Question
I think I'm being really stupid here.
I'm using vbscript. I've connected to an SCCM server
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set connection = locator.ConnectServer("SERVERNAME", "Root\SMS\SITENAME")
I then have a WMI WQL query:
Set Collections = connection.ExecQuery("SELECT LastStatusTime,AdvertisementID,
LastStateName,AdvertisementName
FROM SMS_ClientAdvertisementStatus
INNER JOIN SMS_Advertisement
ON SMS_Advertisement.AdvertisementID = SMS_ClientAdvertisementStatus.AdvertisementID
WHERE LastStateName = 'Succeeded'
AND LastStatusTime > '2012-09-25'")
FOR each Collection in Collections
Collection.LastStatusTime
Collection.AdvertisementID
Next
I think there's a gap in my understanding of WQL. I seem to be able to join these two WQL "tables" in this query, but I can only return values from SMS_ClientAdvertisementStatus.
If I try to return anything from SMS_Advertisement, the table I've joined, I just get an error.
Can you join "tables" in WQL - if they even are tables? Or do I have to have a nested query? Or is there another way of returning data from two tables?
Solution
WQL doesn't support JOINs, but you can use MOF to define WMI classes that contain data from multiple classes. See here:
OTHER TIPS
The WQL
language is just a subset of SQL and doesn't supports the JOIN statement, instead you can use the ASSOCIATORS OF
in some cases.
WQL does support joins. Here is a sample working query, which lists the names of devices which match with collection names. Works in SCCM 2012.
select SMS_R_SYSTEM.Name from SMS_R_System inner join SMS_Collection as Systems on Systems.Name = SMS_R_System.Name
I had a similar issue when trying to use JOIN statements in my PowerShell SCCM / ConfigManager queries and found this to be a great solution:
https://gallery.technet.microsoft.com/scriptcenter/SCCM-2012-WMI-query-with-0daea30c#content
I believe the methods could translate to other languages too.