I am using SCCM to inventory members of the local administrators group on all of my endpoints and I have a SSRS report that I use for reporting. I am trying to modify the query in the report to add some additional data but I am having trouble doing so. I am hoping someone can help provide some insight and point me in the right direction. I am not a SQL guy so I am trying to learn as I go.
I am using SQL 2014. I am using this post https://eskonr.com/2017/03/sccm-configmgr-report-for-local-admins-and-local-group-members/ for querying the local admins on the endpoints and storing the data in the SCCM DB. What I have is a view in the DB called v_GS_LocalGroupMembers0 which contains these columns
[ResourceID] [GroupID] [RevisionID] [AgentID] [TimeStamp] [Account0] [Category0] [Disabled0] [Domain0] [GroupSID0] [Name0][Type0]
The query used in the report looks like this
select sys1.netbios_name0
,lgm.name0 [Name of the local Group]
,lgm.account0 [Account Contained within the Group]
,lgm.domain0 [Domain for Account]
,lgm.type0 [Type of Account]
,scum.TopConsoleUser0 [Top User]
from v_gs_localgroupmembers0 lgm
join v_gs_workstation_status ws on ws.resourceid=lgm.resourceid
join v_r_system sys1 on sys1.resourceid=lgm.resourceid
join v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP scum on scum.ResourceID=lgm.ResourceID
where lgm.name0='Administrators'
order by sys1.netbios_name0, lgm.name0, lgm.account0
I recently modified the query from what it was originally by including a query for the TopConsoleUser0 column in v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP to provide the primary user of the PC. Now my goal is to include the department for the user following what is contained in the department field for each user in AD. I have configured user discovery in SCCM to inventory all users' departments in AD which I found in the department column in the v_R_User view in the SCCM DB. What I can't figure out is how to do it or if this is even possible.
The closest I got where the query doesn't error but also doesn't return any data is
SELECT sys1.Netbios_Name0 AS Hostname,
lgm.Name0 AS [Name of the local Group],
lgm.Account0 AS [Account Contained within the Group],
lgm.Domain0 AS [Domain for Account],
lgm.Type0 AS [Type of Account],
scum.TopConsoleUser0 AS [Top User], usr.department AS Department
FROM v_GS_LocalGroupMembers0 AS lgm INNER JOIN
v_GS_WORKSTATION_STATUS AS ws ON ws.ResourceID = lgm.ResourceID INNER JOIN
v_R_System AS sys1 ON sys1.ResourceID = lgm.ResourceID INNER JOIN
v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP AS scum ON scum.ResourceID = lgm.ResourceID INNER JOIN
v_R_User AS usr ON usr.Name0 = scum.TopConsoleUser0
WHERE (lgm.Name0 = 'Administrators')
ORDER BY Hostname, [Name of the local Group], [Account Contained within the Group]
I'm sure this isn't working because I don't have a common column with matching records between the v_GS_LocalGroupMembers0 and v_R_User views for SQL to match to. Again, not being a SQL guy, I had thought about creating a separate query to get the departments and use a UNION statement to bring it together, but I don't think that will work either. All suggestions are welcome and I'll be happy to provide more info if needed.
Thanks