Last active
April 25, 2018 00:24
-
-
Save R41D3NN/c3d0c834a64690e81563b50de5e46c1e to your computer and use it in GitHub Desktop.
Discovery--Computers-and-Total-Local-Accounts.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
[ou].[DistinguishedName] AS 'OU DN', | |
[c].[ComputerName], | |
COUNT([ca].[ComputerId]) AS 'Total Local Accounts', | |
[CB].[Success], | |
[CB].[LastErrorMessage] | |
FROM [tbComputer] [c] | |
JOIN [tbOrganizationUnit] [ou] ON [c].[OrganizationUnitId] = [ou].[OrganizationUnitId] | |
LEFT JOIN (SELECT * FROM [tbComputerAccount] [ca] WHERE [ca].[ComputerId] IS NOT NULL) [ca] ON [c].[ComputerId] = [ca].[ComputerId] | |
CROSS APPLY | |
( | |
SELECT TOP 1 | |
[csl].[Success], | |
CASE | |
WHEN [csl].[Success] = 1 THEN NULL | |
ELSE [c].[LastErrorMessage] | |
END AS [LastErrorMessage] | |
FROM [tbComputerScanLog] [csl] | |
WHERE [csl].[ComputerId] = [c].[ComputerId] | |
ORDER BY [csl].[ComputerScanLogId] DESC | |
) AS CB | |
GROUP BY | |
[ou].[DistinguishedName], | |
[c].[ComputerId], | |
[c].[ComputerName], | |
[CB].[LastErrorMessage], | |
[CB].[Success] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment