WMI Accounts and Computer Information

Retrieve WMI Services

The following function is the primary one for retrieving WMI objects and services. It returns an object reference to the available Namespace. Note that a lot of this information is also available in VBA through the Environ function.

Function GetWMIService() As Object
' http://msdn.microsoft.com/en-us/library/aa394586(VS.85).aspx
Dim strComputer As String

  strComputer = "."

  Set GetWMIService = GetObject("winmgmts:" _
                              & "{impersonationLevel=impersonate}!\\" _
                              & strComputer & "\root\cimv2")
End Function

Win32_ComputerSystem WMI class

The following sample procedure uses the GetWMIService function, then executes a query (using WQL) to return information from the Win32_ComputerSystem WMI class. Typically only one computer object is returned, but we still need a For Each loop (I tried colSettings(0) and colSettings.Item(0) and got errors).

Sub TestGetWMIComputerInfo()
' http://msdn.microsoft.com/en-us/library/aa394102(VS.85).aspx
Dim objWMI As Object
Dim colSettings As Object
Dim objComputer As Object
Dim strComputerRole As String

  Set objWMI = GetWMIService

  Set colSettings = objWMI.ExecQuery _
                    ("Select * from Win32_ComputerSystem")

  For Each objComputer In colSettings
    With objComputer

      Debug.Print "System Name: " & .Name
      Debug.Print "User Name: " & .UserName
      Debug.Print "Domain: " & .Domain
      Debug.Print "Time Zone: UTC" & _
                  IIf((.CurrentTimeZone / 60) >= 0, "+" & (.CurrentTimeZone / 60), (.CurrentTimeZone / 60))
      Debug.Print "DST Mode On? " & .DaylightInEffect
      ' Note: DNSHostName property is not available in Windows XP and Windows 2000.
      Debug.Print .DNSHostName
      Debug.Print .SystemType

      Select Case .DomainRole
        Case 0
          strComputerRole = "Standalone Workstation"
        Case 1
          strComputerRole = "Member Workstation"
        Case 2
          strComputerRole = "Standalone Server"
        Case 3
          strComputerRole = "Member Server"
        Case 4
          strComputerRole = "Backup Domain Controller"
        Case 5
          strComputerRole = "Primary Domain Controller"
      End Select

      Debug.Print "Role: " & strComputerRole

    End With
  Next objComputer

End Sub

A lot of readers might still be using Windows XP, so note that certain properties are not available to certain operating systems. Much of the above code is merely an adaptation of the VBScript you will find at the referenced MSDN pages.

Win32_Account WMI class

The Win32_Account class also uses the same namespace as Win32_ComputerSystem, but we run the query against Win32_Account instead. This returns information on the accounts available on the local computer. Here is a sample procedure that demonstrates how to iterate the resulting collection of accounts.

Sub TestWMIAccountInfo()
' http://msdn.microsoft.com/en-us/library/aa394061(VS.85).aspx
Dim objWMI As Object
Dim colAccountInfo As Object
Dim acct As Object

  Set objWMI = GetWMIService

  Set colAccountInfo = objWMI.ExecQuery("Select * from Win32_Account")

  For Each acct In colAccountInfo
    With acct

      Debug.Print .Caption
      Debug.Print .Description
      Debug.Print .Domain
      Debug.Print .Name

    End With
  Next acct

End Sub

Site last updated: May 21, 2013

Excel School