SQL: Report showing Targets that can be deleted and the count of systems within them.
select
[_ItemGuid] = i.guid
, [Name] = i.name
, [Owner] = i.[createdby]
, [Count] = (select count(ResourceGuid) from [ResourceTargetMembershipCache] where ResourceTargetGuid = i.guid)
from ResourceTarget t
join vItem i on t.guid = i.guid
where
-- Can be deleted
i.attributes & 16 != 16
-- Is not hidden
and i.attributes & 1 != 1
-- Bad target name 'Windows Computers with Inventory Plug-in'
and i.name != '''Windows Computers with Inventory Plug-in'''
order by 1