SCCM 2007 - Find Date of Last Patch Installed on the Server

I am looking for the best way to collect the date of the last patch successfully installed to the server, which indicates when the server was last patched.

In Powershell, we have used the following script to accomplish this task:

Foreach ($Server in $colServers)
	{
    Try
}    	{
	    Get-HotFix -ComputerName $Server.Name | `
	    Where {$_.InstalledOn} | `
	    Sort InstalledOn -Descending | `
	    Select CSname, @{Name = "Installed"; Expression = {"{0:M/dd/yyyy}" -f [datetime]$_.InstalledOn.Tostring()}} -First 1
	}
    Catch 
        {
        Write-Host ($Server.Name + ":  " + $Error[0])
    }

Open in new window


I need to collect this information from the SCCM database using a SQL statement.  One query I looked at as a possibility was the following:

SELECT DISTINCT v_R_System.Name0 AS 'Machine Name', 

      v_gs_patchstatusEx.ID AS 'Bulletin ID#', 

      v_gs_patchstatusEx.QNumbers as 'QNumber', 

      v_gs_patchstatusEx.Title as 'Title', 

      v_gs_patchstatusEx.LastStateName as 'Last State',  

      v_gs_patchstatusEx.LastStatusMessageIDName as 'Last Status', 

      v_gs_patchstatusEx.LastStatusTime as 'Last Status Time'
 
FROM v_gs_patchstatusEx 

INNER JOIN v_R_System on v_R_System.ResourceID = v_gs_patchstatusEx.ResourceID 

INNER JOIN v_ClientCollectionMembers on v_ClientCollectionMembers.ResourceID = v_R_System.ResourceID
 
INNER JOIN v_UpdateComplianceStatus on v_UpdateComplianceStatus.ResourceID = v_R_System.ResourceID
 
WHERE (v_gs_patchstatusEx.id <> 'None') AND 

        (v_gs_patchstatusEx.LastStateName = 'Install Verified') AND 
        
        (DATEDIFF(hh,v_GS_PatchStatusEx.LastStatusTime,Getdate())<=48 )AND
 
        (v_UpdateComplianceStatus.Status=2) AND 

        (v_ClientCollectionMembers.CollectionID='SMS00001')
 
ORDER BY v_gs_patchstatusEx.LastStatusTime, v_gs_patchstatusEx.ID DESC

Open in new window


However, I need to modify it to only collect the date of the most recently installed patch.  I think the Powershell script above targets the Win32_QuickFixEngineering WMI class and I would like to continue using this same functionality in a SQL statement if it can be done.

The most important thing is accuracy.  I must have the date of the latest patch successfully installed on the server.
LVL 3
isd503Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leon TaljaardSystem Center Specialist - ArchitectureCommented:
Hi

Not sure if something like this might help ?

select b.Name,b.CollectionID,a.DisplayName0,
CONVERT(VARCHAR, DATEADD(hh, 2, a.InstallDate0),103) as 'Install Date'
from dbo.v_Add_Remove_Programs a join dbo.v_FullCollectionMembership b on a.ResourceID = b.ResourceID
where b.Name like 'Computer Name'
0
isd503Author Commented:
We are making some changes to SCCM to be able to collect accurate SQL and Microsoft patch data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isd503Author Commented:
I could not find another way to get this done accurately.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.