When running a Power BI report, you are prompted to trust the SQL native queries. Here we provide the queries, and an explanation on their usage.
SQL Queries
Client Info
Collection of information about devices including what version of the operating system they are running, and FQDN. Originally we used to gather this from a different view, however this caused numerous reports of duplicate values for large environments due to maintenance tasks not running quickly enough. Instead we dynamically build the content. We also have to handle duplicates caused by OS caption.
WITH NewestOS AS (SELECT GS_OS_Data.ResourceID , TimeStamp , Caption0FROM v_GS_Operating_System as GS_OS_DataINNER JOIN (SELECT ResourceID , MAX(TimeStamp) as MaxDateFROM v_GS_Operating_SystemGROUP BY ResourceID) MaxFilter on GS_OS_Data.ResourceID = MaxFilter.ResourceID and MaxFilter.MaxDate = GS_OS_Data.TimeStamp)SELECT v_R_System.ResourceID , v_R_System.Name0 , v_R_System.Name0 +'.'+ v_R_System.Full_Domain_Name0 as Resource_Names0 , v_R_System.Resource_Domain_OR_Workgr0 AS'DOMAIN' , CASE v_R_System.Client0WHEN1THEN'INSTALLED'ELSE'MISSING'ENDAS ClientStatus , v_R_System.Client_Version0 , v_R_System.Last_Logon_Timestamp0 , CASE NewestOS.Caption0WHENNULLTHEN v_R_System.Operating_System_Name_and0ELSE NewestOS.Caption0ENDAS OSNAME , V_GS_System.SystemRole0FROM v_R_SystemLEFT OUTER JOIN NewestOS on v_R_System.ResourceID = NewestOS.ResourceIDLEFT OUTER JOIN v_GS_System ON v_R_System.ResourceID = v_GS_System.ResourceIDWHERE v_R_System.Operating_System_Name_and0 LIKE'%Microsoft%'
Compliance Info
Collect patch compliance data. Technically the 0 management is not required here, as 0 should never appear in v_Update_ComplianceStatus
We also filter out some information to ensure we are avoiding accidentally capturing software update groups.
This query gathers the names of each software update group, and creates a dummy row, for updates that are not in any software update group. This is done to handle a use case with Power BI.
SELECT v_UpdateInfo.CI_ID, v_UpdateInfo.TitleFROM v_UpdateInfoWHERE v_UpdateInfo.CIType_ID ='9'UNION ALLSELECT'0', '--- Not Limited by Sug ---'
SUG Relation Map
This builds what we call the relation map, and tracks what updates are a member of what group. This is how we then are able to filter our report by software update groups. This data is used as a connection point in the PowerBI data model to join data together.
WITH SUGInfo AS(SELECT v_UpdateInfo.CI_IDFROM v_UpdateInfoWHERE v_UpdateInfo.CIType_ID ='9')SELECT v_CIRelation.FromCIID, v_CIRelation.ToCIIDFROM SUGInfoLEFT OUTER JOIN v_CIRelation ON SUGInfo.CI_ID = v_CIRelation.FromCIIDUNION ALLselect'0'AS [FromCIID], CI_ID FROM v_UpdateInfowhere CIType_ID !=9
Collection Membership
Collections, often represent logical groupings of machines we really care about. As a result, we use this query to build out what machines are a member of what collection so we can allow filtering based on the collection names.
While knowing a CI_ID of an update is nice, you typically want more detailed information. This, collects data for things like the URL for information about the update, the date it was released, and more.
SELECT v_UpdateInfo.CI_ID , v_UpdateInfo.CI_UniqueID , v_UpdateInfo.ArticleID , v_UpdateInfo.Title , v_UpdateInfo.InfoURL , v_UpdateInfo.DatePosted , v_UpdateInfo.DateCreated , CASE v_UpdateInfo.IsDeployedWHEN1THEN'TRUE'ELSE'FALSE'ENDAS DeploymentStateFROM v_UpdateInfoWHERE v_UpdateInfo.ArticleID IS NOT NULLAND v_UpdateInfo.ArticleID !=''
Category Info
Some times it really helps to be able to filter a report by the patching vendors. This section gathers who the original author of the patch was.
WITH CATS as(select all SMS_UpdateCategoryInstance.LocaleID ,SMS_UpdateCategoryInstance.CategoryInstanceName ,SMS_UpdateCategoryInstance.CategoryInstance_UniqueID ,SMS_UpdateCategoryInstance.CategoryInstanceIDfrom fn_ListUpdateCategoryInstances(1033) AS SMS_UpdateCategoryInstance where (SMS_UpdateCategoryInstance.CategoryTypeName =N'Company'AND SMS_UpdateCategoryInstance.AllowSubscription =1))Select CATS.CategoryInstanceName , v_updateINfo.CI_IDfrom fn_ListCICategoriesAll(1033) as CI_LISTINNER Join CATS on CI_LIST.CategoryInstanceID = cats.CategoryInstanceIDINNER JOIN v_updateinfo on v_updateinfo.ci_id = ci_list.ci_id