Tag Archives: WMI

Listing All The WMI Class Properties With VBA

In my past WMI articles:

I’ve always shown you how to retrieve a specific element, specific value. Today, however, I thought I’d share a very simple ‘tool’ that I created to help me see what a class could provide me as information. Yes, there is the documentation, but sometimes, seeing the real-life output from your own device greatly aid in the learning process. Understanding what the difference is between DeviceId, Caption and Name … is much easier to do when you see, for yourself, the concrete values returned by WMI.

As such, I developed a very simple function that will enumerate all the properties of the specified WMI Class, thus allowing you to review the output, and this in turn can help you build your own procedure to retrieve just the right property value.

Continue reading

Getting The Hard Drive Serial Number With VBA

I was trying to help in the following Answers forum thread

In which Wahedi is inquirying about retrieving the HDD serial number.

I knew I had such code stored away because I had done this on a personal project many years ago.  I also knew I had previously provided code to retrieve the BIOS serial number on this blog which employs the same basic approach.

So I went digging and found 2 functions that might be of interest to some of you out there.

Continue reading

VBA – Get The Computer Name

Very similarily to determining the current user’s username, it can be very useful to be able to determine the current PC’s name. This could be to:

  • include with your error loging
  • use to lock down your application to running on only certain PCs
  • use as a configuration parameter (if PCA then use the folder, if PCB use that folder, ….)
  • etc.

Today, I thought I’d quickly cover a couple way this can be accomplished:

Continue reading

VBA – Get Processor Id

I was helping someone out in a forum that was trying to determine the computer’s Processor Id to use it as part of a registration system.

Once again WMI automation can enable us to get this information. Below was the function I put together.

Continue reading

List the Installed PC Updates

Once again, building upon my previous post, Determine if an Update has been Installed or Not, I transformed my initial IsQuickFixNoInstalled() function into a more versatile function to list all the updates installed. One could easily use it to populate a listbox or output its results to a text file for easier review.

'---------------------------------------------------------------------------------------
' Procedure : EnumerateQuickFixes
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Enumerate the updates installed on the specified 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:
' ~~~~~~~~~~~~~~~~
' sHost     : Name, IP address of the computer to check
'             Omit this input variable if checking the local PC
'
' Usage:
' ~~~~~~
' ? EnumerateQuickFixes
' ? EnumerateQuickFixes("172.12.243.195")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2011-07-22                  Initial Release
'---------------------------------------------------------------------------------------
Public Function EnumerateQuickFixes(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
    Dim oQuickFix             As Object    'Individual QuickFix

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    sWMIQuery = "SELECT * " & _
                "FROM Win32_QuickFixEngineering"
    Set oQuickFixes = oWMI.ExecQuery(sWMIQuery)
    For Each oQuickFix In oQuickFixes
        Debug.Print oQuickFix.HotFixID, oQuickFix.Description, oQuickFix.Caption, oQuickFix.InstalledOn  ', oQuickFix.InstallDate, oQuickFix.Name, oQuickFix.Status
    Next

Error_Handler_Exit:
    On Error Resume Next
    Set oQuickFix = Nothing
    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: EnumerateQuickFixes" & 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.

Determine if an Update has been Installed or Not

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.

VBA – Computer Uptime

I’ve been dealing with an ongoing issue with Access 2010 where calculated controls do not display values.

When you start Microsoft Access 2010 on a computer that has not been restarted for a long time (for example, 24 days).

fields that are bound to expressions on forms may not be recalculated when the computer has not been restarted for a long time.

Taken from: https://support.microsoft.com/en-us/kb/2965300

Continue reading

VBA – Determine the Installed OS

When troubleshooting various issues, one of the common thing one does is try to get some information about the PC itself.  Things like:

  • OS
  • MS Office

It can be difficult at time to get such information from users depending on their skill level and as such, a while back I had post in a forum a function which use WMI to get the OS name and version.  I took my old function and cleaned it up and provide it below should you wish to be able to easily determine your users’ OS easily.

'---------------------------------------------------------------------------------------
' Procedure : getOperatingSystem
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Return the active OS details
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sHost     : Name/IP Address of the PC to query assuming you have the rights to do so
'               optional, so by leaving it blank it will query the local computer
'
' Usage:
' ~~~~~~
' ? getOperatingSystem()  -> Microsoft Windows 7 Ultimate  6.1.7601 (64-bit)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2012-09-27              Initial Release
' 2         2016-09-24              Code Cleanup and standardization
' 3         2018-08-30              Added OS bitness to returned value
'                                   Updated Copyright
'---------------------------------------------------------------------------------------
Public Function getOperatingSystem(Optional sHost As String = ".") As String
    'Win32_OperatingSystem -> https://msdn.microsoft.com/en-us/library/aa394239%28v=vs.85%29.aspx
    On Error GoTo Error_Handler
    Dim oWMI                  As Object    'WMI object to query about the PC's OS
    Dim oOSs                  As Object    'Collection of OSs
    Dim oOS                   As Object    'Individual OS

    Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHost & "\root\cimv2")
    Set oOSs = oWMI.ExecQuery("SELECT Caption, Version, OSArchitecture FROM Win32_OperatingSystem")

    For Each oOS In oOSs    'Enumerate each OS provided by WMI
        getOperatingSystem = getOperatingSystem & oOS.Caption & " " & oOS.Version & _
                             " (" & oOS.OSArchitecture & "), "
    Next
    getOperatingSystem = Left(getOperatingSystem, Len(getOperatingSystem) - 2)    'Remove the last ", "

Error_Handler_Exit:
    On Error Resume Next
    If Not oOS Is Nothing Then Set oOS = Nothing
    If Not oOSs Is Nothing Then Set oOSs = Nothing
    If Not oWMI Is Nothing Then Set oWMI = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: getOperatingSystem" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

If you have some type of global error handler, this could be a valuable piece of information to include as part of the error reporting process.

MS Access – VBA – Kill a Process

Every once in a while, programs do not shutdown properly and thus cause headaches. So it can become necessary to terminate the process. Below are a couple different approaches you can employ to terminate, forcibly close, a process/program.
 
Continue reading