Create SCCM device collection based on last logged on users who are members of an AD security group

active-directoryquerysccmsccm-2012wmi

I'm trying to create a device collection in SCCM 2012 which contains only the devices who are used by the users who are members of a certain User AD Security Group. I tried to accomplish this by first making a query for all the users in the group, which worked fine:

select SMS_R_User.UserGroupName, 
SMS_R_User.UserName 
from  SMS_R_User where SMS_R_User.UserGroupName = "Domain\\AD_Group"

Then I created a collection of devices with a query rule where the criteria was that if the last logged on user of the device was part of the subselected values of the first group query I made, then those devices would be added to the collection. The query rule:

select *  from  SMS_R_System 
where SMS_R_System.LastLogonUserName 
in (select SMS_R_User.UserGroupName, SMS_R_User.UserName 
from  SMS_R_User where SMS_R_User.UserGroupName = "Domain\\AD_Group")

However when I try to save the query rule Configuration Manager says that the query is not valid. I'm not exactly sure what I'm doing wrong. Maybe there is a better approach?

Best Answer

Try this one:

SELECT * FROM SMS_R_SYSTEM 
  INNER JOIN SMS_R_uSER 
  ON SMS_r_system.LastLogonUserName = SMS_R_uSER.FullUserName 
WHERE SMS_R_uSER.SecurityGroupName = 'Domain\\AD_Group';