Continuing on my previous post, entitled VBA – Computer Uptime, about Access 2010 bug, I needed to find a way to identify whether or not certain updates had been installed on the server or not and did not have access to the admin console/control panel/etc.
Once again, I set out to create a function to ask the system and knew WMI could help! Below is the function I came up with:
'---------------------------------------------------------------------------------------
' Procedure : IsQuickFixNoInstalled
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Determine if an OS update has been installed or not on a PC
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sQuickFixNo:The Update/Hotfix/KB number - just the numeric part
' sHost : Name, IP address of the computer to check
' Omit this input variable if checking the local PC
'
' Usage:
' ~~~~~~
' IsQuickFixNoInstalled("2965300")
' IsQuickFixNoInstalled("2965300", "172.12.243.195")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2011-07-22 Initial Release
'---------------------------------------------------------------------------------------
Public Function IsQuickFixNoInstalled(sQuickFixNo As String, Optional sHost As String = ".") As Boolean
'only seems to report on Windows updates and nothing else (ie not office updates)
'Ref: https://msdn.microsoft.com/en-us/library/aa394391(v=vs.85).aspx
On Error GoTo Error_Handler
Dim oWMI As Object 'WMI object to query about the PC's OS
Dim sWMIQuery As String 'WMI Query
Dim oQuickFixes As Object 'List of QuickFixes matching our WMI Query
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
sWMIQuery = "SELECT HotFixID " & _
"FROM Win32_QuickFixEngineering " & _
"WHERE ((HotFixID = 'Q" & sQuickFixNo & "') OR (HotFixID = 'KB" & sQuickFixNo & "'))"
Set oQuickFixes = oWMI.ExecQuery(sWMIQuery)
If oQuickFixes.count > 0 Then
IsQuickFixNoInstalled = True 'It is installed
Else
IsQuickFixNoInstalled = False 'It is not installed
End If
Error_Handler_Exit:
On Error Resume Next
Set oQuickFixes = Nothing
Set oWMI = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: IsQuickFixNoInstalled" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Do note that the function takes a couple seconds to return a value.