Wednesday, May 25, 2016

SQL Query for Device Information

Lovely query for your Device information overview

SET NOCOUNT ON;

-- Declaration

  
--Devices
SELECT 
             COALESCE(Computer_System_DATA.Name0, '') AS DeviceName,
COALESCE(v_gS_operating_system.Caption0, '') + ' ' + COALESCE(v_gS_operating_system.CSDVersion0, '') + ' ' + COALESCE(v_gS_operating_system.Version0, '') AS OperatingSystem,
CASE
when v_gS_operating_system.Caption0 like '%server%' then 'Server'
  when System_Enclosure_data.ChassisTypes0 = 1 then 'Other'
when System_Enclosure_data.ChassisTypes0 = 2 then 'Unknown'
when System_Enclosure_data.ChassisTypes0 = 3 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 4 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 5 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 6 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 7 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 8 then 'Desktop'
when System_Enclosure_data.ChassisTypes0 = 9 then 'Laptop'
when System_Enclosure_data.ChassisTypes0 = 10 then 'Laptop'
when System_Enclosure_data.ChassisTypes0 = 11 then 'Hand Held'
when System_Enclosure_data.ChassisTypes0 = 12 then 'Docking Station'
when System_Enclosure_data.ChassisTypes0 = 13 then 'All in One'
when System_Enclosure_data.ChassisTypes0 = 14 then 'Sub Notebook'
when System_Enclosure_data.ChassisTypes0 = 15 then 'Space-Saving'
when System_Enclosure_data.ChassisTypes0 = 16 then 'Lunch Box'
when System_Enclosure_data.ChassisTypes0 = 17 then 'Main System Chassis'
when System_Enclosure_data.ChassisTypes0 = 18 then 'Expansion Chassis'
when System_Enclosure_data.ChassisTypes0 = 19 then 'SubChassis'
when System_Enclosure_data.ChassisTypes0 = 20 then 'Bus Expansion Chassis'
when System_Enclosure_data.ChassisTypes0 = 21 then 'Peripheral Chassis'
when System_Enclosure_data.ChassisTypes0 = 22 then 'Storage Chassis'
when System_Enclosure_data.ChassisTypes0 = 23 then 'Rack Mount Chassis'
when System_Enclosure_data.ChassisTypes0 = 24 then 'Sealed-Case PC'
end as 
  DeviceType,
'' as ServicePack,
coalesce(Network_DATA.IPAddress0,'') as IPAddress,
  case
when System_Enclosure_DATA.SerialNumber0 like 'VMware%' then 'Virtual'
when Computer_System_DATA.Model0 like 'VMWare%' then 'Virtual'
when Computer_System_DATA.Manufacturer0 like 'VMWare%' then 'Virtual'
when System_Enclosure_DATA.SerialNumber0 is not null then 'Physical'
when Computer_System_DATA.Manufacturer0 is not null then 'Physical'
when System_Enclosure_DATA.Model0 is not null then 'Physical'
else null
 end as PhysicalOrVirtual,
             COALESCE(Computer_System_DATA.Manufacturer0, '') AS Make,
             COALESCE(Computer_System_DATA.Model0, '') AS Model,
             COALESCE(System_Enclosure_DATA.SerialNumber0, '') AS serial_number,
COALESCE(LTRIM(Processor_DATA.Name0), 'Unknown') AS CPUName,
             COALESCE(cpus.cpu_count, '') AS NumberPhysicalCPUs,
sum(cpus.corecounts) as NumberLogicalCPUs,
             COALESCE(PC_Memory_DATA.TotalPhysicalMemory0 / 1024, '') AS RAMinBytes,
RS.User_Name0 as LastLoggedOnUser,
WS.LastHWScan as LastSeenDate,
null as Environment,
null as PhysicalHost
FROM         v_gs_Computer_System Computer_System_DATA
LEFT JOIN    v_GS_NETWORK_ADAPTER_CONFIGUR Network_DATA
                 ON (Computer_System_DATA.Resourceid = Network_DATA.Resourceid AND Network_DATA.Index0 = 1)

LEFT JOIN    v_gS_operating_system 
                 ON (Computer_System_DATA.Resourceid = v_gS_operating_system.Resourceid
                     AND v_gS_operating_system.BootDevice0 like '\Device\HarddiskVolume%')
LEFT JOIN    v_gs_Processor Processor_DATA
                 ON (Computer_System_DATA.Resourceid = Processor_DATA.Resourceid AND Processor_DATA.DeviceID0 = 'CPU0')
LEFT JOIN    v_GS_X86_PC_MEMORY pc_memory_DATA
                 ON (Computer_System_DATA.Resourceid = PC_Memory_DATA.Resourceid)
LEFT JOIN    v_gs_Disk disk_DATA
                 ON (Computer_System_DATA.Resourceid = Disk_DATA.Resourceid AND Disk_DATA.Index0 = 0)
LEFT JOIN    v_gs_Video_Controller Video_Controller_DATA
                 ON (Computer_System_DATA.Resourceid = Video_Controller_DATA.Resourceid
                     AND Video_Controller_DATA.DeviceID0 = 'VideoController1')
LEFT JOIN    v_gs_PC_BIOS PC_BIOS_DATA
                 ON (Computer_System_DATA.Resourceid = PC_BIOS_DATA.Resourceid)
LEFT JOIN    (
                 SELECT ResourceID,
                        COUNT(*) AS cpu_count,
sum(numberofCores0) as corecounts
                 FROM v_gs_Processor
                 GROUP BY resourceID
             ) cpus ON (Computer_System_DATA.Resourceid = cpus.Resourceid)
LEFT JOIN    v_gs_System_Enclosure System_Enclosure_data
                 ON (Computer_System_DATA.Resourceid = System_Enclosure_DATA.Resourceid AND System_Enclosure_DATA.GroupID = 1)
LEFT JOIN    v_R_System_valid RS
                 ON (Computer_System_DATA.Resourceid = RS.ResourceID)
LEFT JOIN    v_GS_WORKSTATION_STATUS WS
                 ON (RS.ResourceID = WS.ResourceID)
GROUP BY     Computer_System_DATA.Resourceid,
             COALESCE(Computer_System_DATA.Name0, ''),
             Computer_System_DATA.UserName0,
             Network_DATA.IPAddress0,
             v_gS_operating_system.InstallDate0,
             Processor_DATA.Name0,
             Processor_DATA.NormSpeed0,
             PC_Memory_DATA.TotalPhysicalMemory0,
             Disk_DATA.Size0,
             Video_Controller_DATA.Description0,
             PC_BIOS_DATA.Description0,
             System_Enclosure_DATA.ChassisTypes0,
            v_gS_operating_system.Caption0,
v_gS_operating_system.organization0,
             v_gS_operating_system.CSDVersion0,
             v_gS_operating_system.Version0,
             cpus.cpu_count,
             System_Enclosure_DATA.SMBIOSAssetTag0,
             Processor_DATA.DeviceID0,
             Computer_System_DATA.Manufacturer0,
             Computer_System_DATA.Model0,
             System_Enclosure_DATA.SerialNumber0,
             PC_BIOS_DATA.SerialNumber0,
             WS.LastHWScan,
System_Enclosure_DATA.Model0,
RS.User_Name0

Wednesday, May 04, 2016

Useful SQL Reports

Last Logon (since xxtime):

SELECT 
       [Netbios_Name0] AS 'Computer Name'
 ,[Full_Domain_Name0] AS 'Domain'
 ,[Last_Logon_Timestamp0] AS 'Last Logon'
 ,[Distinguished_Name0] AS 'Distinguished Name'
      ,[User_Domain0] AS 'User Logon Domain'
      ,[User_Name0] AS 'User Name'
      ,[Operating_System_Name_and0] AS 'OS'
  
  FROM [CM_I01].[dbo].[v_R_System] 

  WHERE convert(varchar(10),Last_Logon_Timestamp0,121)>='2016-01-01' AND Client0 = '1'

SQL Reports: Microsoft Office Suites

The difficulty in creating a report from the out-of-box reports in ConfigMgr is the wonderful array of product names that Microsoft uses. That is, one size doesn't fit all. You can't just use LIKE 'Microsoft Office%':

  • Microsoft Office 97 Professional Edition
  • Microsoft Office 365 ProPlus - en-us
  • Microsoft Office 365 ProPlus - ko-kr
  • Microsoft Office OSM MUI (English) 2013
  • Microsoft Office OSM UX MUI (English) 2013
  • Microsoft Office Shared Setup Metadata MUI (English) 2013
  • Microsoft Office Shared 64-bit Setup Metadata MUI (English) 2013
  • Microsoft Office 64-bit Components 2013
  • Microsoft Office Shared 64-bit MUI (English) 2013
  • Microsoft Office Home and Business 2010
  • Microsoft Office Professional Edition 2003
  • Microsoft Office Professional Edição 2003
In a word: Urgh! So how do we report on count of all the version installed? Firstly, you should get a report of all the versions that your estate has installed. Run a query against your CM DB and export to a file:
SELECT [ResourceID]
      ,[GroupID]
      ,[TimeStamp]
      ,[ARPDisplayName0]
      ,[InstallDate0]
      ,[InstallDirectoryValidation0]
      ,[InstalledLocation0]
      ,[InstallSource0]
      ,[InstallType0]
      ,[Language0]
      ,[LocalPackage0]
      ,[MPC0]
      ,[OsComponent0]
      ,[PackageCode0]
      ,[ProductID0]
      ,[ProductName0]
      ,[ProductVersion0]
      ,[Publisher0]
      ,[RegisteredUser0]
      ,[ServicePack0]
      ,[SoftwareCode0]
      ,[UninstallString0]
      ,[UpgradeCode0]
      ,[VersionMajor0]
      ,[VersionMinor0]
  FROM [CM_I01].[dbo].[v_GS_INSTALLED_SOFTWARE] WHERE ARPDisplayName0 like 'Microsoft Office%'



Now you need to prune out all the individual product names (Excel, Powerpoint, Outlook), Proofing, Shared Components etc). This I did exporting the query to Excel and applying a filter. Took about 7 minutes. This will leave you with the Product list for the Office suites themselves:

Microsoft Office 97 Professional Edition
Microsoft Office 365 ProPlus - en-us
Microsoft Office 365 ProPlus - ko-kr
Microsoft Office Home and Business 2010
Microsoft Office 365 ProPlus - de-de
Microsoft Office Professional Edition 2003
Microsoft Office Professional Edição 2003
Microsoft Office Standard Edition 2003
Microsoft Office Enterprise 2007
Microsoft Office Professional Plus 2007
Microsoft Office Professional Plus 2010
Microsoft Office Standard 2010
Microsoft Office Home and Student 2010
Microsoft Office Click-to-Run 2010
Microsoft Office Starter 2010 - English
Microsoft Office Professional Plus 2013
Microsoft Office Standard 2013
Microsoft Office Professional Plus 2016
Microsoft Office XP Professional
Microsoft Office 2000 SR-1 Professional

Now you can get to the good stuff. The SQL report. In my report I want to know all the suites installed and reported on from the start of 2016, grouped by count and sorted by descending value. I won't go through how to create a report and how to pretty is up, but here's the query. You should paste in your own results for the suites you're looking for: 


Select ARPDisplayName0 AS "Office Product Suite", count (*) AS "Count" FROM (

SELECT  

       [ResourceID]

      ,[GroupID]

      ,[TimeStamp]

      ,[ARPDisplayName0]

       ,[InstallDate0]

      ,[InstallDirectoryValidation0]

      ,[InstalledLocation0]

      ,[InstallSource0]
      ,[InstallType0]
      ,[Language0]
      ,[LocalPackage0]
      ,[MPC0]
      ,[OsComponent0]
      ,[PackageCode0]
      ,[ProductID0]
      ,[ProductName0]
      ,[ProductVersion0]
      ,[Publisher0]
      ,[RegisteredUser0]
      ,[ServicePack0]
      ,[SoftwareCode0]
      ,[UninstallString0]
      ,[UpgradeCode0]
      ,[VersionMajor0]
      ,[VersionMinor0]
  FROM [CM_I01].[dbo].[v_GS_INSTALLED_SOFTWARE]
  WHERE convert(varchar(10),timestamp,121)>='2016-01-01' AND ARPDisplayName0 IN (
  'Microsoft Office 97, Professional Edition',
  'Microsoft Office 365 ProPlus - en-us',
  'Microsoft Office 365 ProPlus - ko-kr',
  'Microsoft Office Home and Business 2010',
  'Microsoft Office 365 ProPlus - de-de',
  'Microsoft Office Professional Edition 2003',
  'Microsoft Office Professional Edição 2003',
  'Microsoft Office Standard Edition 2003',
  'Microsoft Office Enterprise 2007',
  'Microsoft Office Professional Plus 2007',
  'Microsoft Office Professional Plus 2010',
  'Microsoft Office Standard 2010',
  'Microsoft Office Home and Student 2010',
  'Microsoft Office Click-to-Run 2010',
  'Microsoft Office Starter 2010 - English',
  'Microsoft Office Professional Plus 2013',
  'Microsoft Office Standard 2013',
  'Microsoft Office Professional Plus 2016',
  'Microsoft Office XP Professional',
  'Microsoft Office 2000 SR-1 Professional')) a

group by ARPDisplayName0
order by count (*) DESC


Management are very happy bunnies, and your SQL-Fu has improved by +2