SCCM Hardware inventory custom queries 2 ( Specified RAM, Hard Drive details)

 The SQL query provided in this post is designed to extract a wealth of information from your SCCM database. Here’s a breakdown of what it does:

  1. Hostname and Model Information:
    • Retrieves the NetBIOS name (hostname) and the model of each PC.
  2. Operating System Details:
    • Fetches the OS version and build number, which is essential for ensuring compliance and identifying systems that need updates.
  3. Processor Information:
    • Gathers details about the processor, including its name, number of cores, and logical processors.
  4. Memory Configuration:
    • Concatenates information about all RAM slots, including capacity, type, and bus speed, providing a complete picture of the system’s memory configuration.
  5. Storage Details:
    • Retrieves information about the type and model of each drive, as well as the total and free space on each logical disk.
  6. Total Memory and Disk Size:
    • Calculates the total physical memory and the sum of all logical disk sizes, giving you an overview of the system’s storage capacity.





 

DECLARE @CollectionID NVARCHAR(8);

SET @CollectionID = 'SMS00001'; -- All Systems, change this to your target collection if needed

 

SELECT DISTINCT

    sys.Netbios_Name0 AS 'Hostname',

    comp.Model0 AS 'PC Model Name',

   

    -- OS Information

    os.Caption0 AS 'OS Version',

    os.BuildNumber0 AS 'OS Build',

   

    -- Processor Details

    processor.Name0 AS 'Processor Name',

    processor.NumberOfCores0 AS 'Processor Cores',

    processor.NumberOfLogicalProcessors0 AS 'Logical Processors',

   

    -- Memory Information - Using STUFF/FOR XML to concatenate all RAM slots for a system

    STUFF(

        (SELECT ', ' + 'Slot: ' + mem.BankLabel0 +

                ', Capacity: ' + CAST(mem.Capacity0/1024 AS VARCHAR(10)) + ' GB' +

                ', Type: ' + CASE

                    WHEN mem.FormFactor0 = 8 THEN 'DIMM'

                    WHEN mem.FormFactor0 = 12 THEN 'SODIMM'

                    ELSE 'Other'

                END

         FROM v_GS_PHYSICAL_MEMORY mem

         WHERE mem.ResourceID = sys.ResourceID

         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),

        1, 2, ''

    ) AS 'RAM Configuration',

   

    -- RAM Bus Speed

    STUFF(

        (SELECT DISTINCT ', ' + CAST(mem.Speed0 AS NVARCHAR(10)) + ' MHz'

         FROM v_GS_PHYSICAL_MEMORY mem

         WHERE mem.ResourceID = sys.ResourceID

         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),

        1, 2, ''

    ) AS 'RAM Bus Speed',

   

    -- Hard Drive Type/Model Information with unique drive letters

    STUFF(

        (SELECT DISTINCT ', ' +

                ISNULL(pd.Model0, 'Unknown Model') + ' (' +

                CASE pd.MediaType0

                    WHEN 0 THEN 'Unspecified'

                    WHEN 3 THEN 'HDD'

                    WHEN 4 THEN 'SSD'

                    WHEN 5 THEN 'SCM'

                    ELSE 'Unknown'

                END + ') ' +

                STUFF(

                    (SELECT DISTINCT ',' + ld_sub.DeviceID0

                     FROM v_GS_LOGICAL_DISK ld_sub

                     LEFT JOIN v_GS_PHYSICAL_DISK pd_sub ON ld_sub.ResourceID = pd_sub.ResourceID

                     WHERE ld_sub.ResourceID = sys.ResourceID

                     AND (pd_sub.Model0 = pd.Model0 OR (pd_sub.Model0 IS NULL AND pd.Model0 IS NULL))

                     AND (pd_sub.MediaType0 = pd.MediaType0 OR (pd_sub.MediaType0 IS NULL AND pd.MediaType0 IS NULL))

                     GROUP BY ld_sub.DeviceID0

                     FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),

                    1, 1, ''

                )

         FROM v_GS_LOGICAL_DISK ld

         LEFT JOIN v_GS_PHYSICAL_DISK pd ON ld.ResourceID = pd.ResourceID

         WHERE ld.ResourceID = sys.ResourceID

         GROUP BY pd.Model0, pd.MediaType0

         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),

        1, 2, ''

    ) AS 'Drive Types',

   

    -- Hard Drive Space Information with unique drive letters

    STUFF(

        (SELECT DISTINCT ', ' + ld_inner.deviceid0 + ' (' +

                CAST(ld_inner.Size0/1024 AS VARCHAR(20)) + ' GB, Free: ' +

                CAST(ld_inner.FreeSpace0/1024 AS VARCHAR(20)) + ' GB)'

         FROM v_GS_LOGICAL_DISK AS ld_inner

         WHERE ld_inner.ResourceID = sys.ResourceID

         GROUP BY ld_inner.deviceid0, ld_inner.Size0, ld_inner.FreeSpace0

         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),

        1, 2, ''

    ) AS 'Drive Space',

   

    -- Total Physical Memory (as in your reference query)

    m.TotalPhysicalMemory0 / 1000 AS 'Total Memory (MB)',

   

    -- Full Hard Disk Size (Summing all logical disk sizes)

    (SELECT SUM(ld_inner.Size0) / 1024

     FROM v_GS_LOGICAL_DISK ld_inner

     WHERE ld_inner.ResourceID = sys.ResourceID) AS 'Total Hard Disk Size (GB)'

 

FROM v_R_System sys

INNER JOIN v_FullCollectionMembership fcm ON sys.ResourceID = fcm.ResourceID

LEFT JOIN v_GS_COMPUTER_SYSTEM comp ON sys.ResourceID = comp.ResourceID

LEFT JOIN v_GS_PROCESSOR processor ON sys.ResourceID = processor.ResourceID

LEFT JOIN v_GS_OPERATING_SYSTEM os ON sys.ResourceID = os.ResourceID

LEFT JOIN v_GS_X86_PC_MEMORY m ON sys.ResourceID = m.ResourceID

 

WHERE fcm.CollectionID = @CollectionID

AND fcm.ResourceType = 5 -- Devices Only

 

ORDER BY sys.Netbios_Name0;

----------------------------------------------------------------------------------------------------------------------

Popular Posts

Windows 11 24H2 Upgrade using Intune Feature Updates Policy

Deploying a Script through Intune to a Linux PC

Deleting Registry.pol and Forcing Group Policy Update Using a Batch File - Deploy through SCCM

Removing Obsolete Computer Records from Active Directory Using PowerShell (Only Windows Client Versions)