Windows Management Instrumentation (WMI) Tutorial

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    Windows Management Instrumentation (WMI) Tutorial

    Introduction
    Windows Management Instrumentation (WMI) is a set of extensions to Windows that provides information and notification about the computer system. WMI allows scripting languages like VBScript or Windows PowerShell to manage Microsoft Windows computers locally and remotely. WMI is preinstalled in Windows 2000 and newer OSs. It is available as a download for Windows NT, Windows 95 and Windows 98.

    WMI allows you to do varied things including but not limited to:
    • Get a list of running processes
    • Get a list of installed programs
    • Get a lsit of startup programs
    • Get disk usage
    • Get the make, model, and serial number of the computer
    • Get the current memory and CPU usage
    • Get notifications when a new process is created
    • Get notifications when the computer is going into or coming out of sleep mode
    • Create and kill processes
    • Shut down a computer


    You can find more information about WMI from Microsoft's documentation located here: WMI Reference (Windows). Thanks to NeoPa for the link.

    Sample VBA implementation
    The following is a sample implementation in VBA that monitors when a computer is about to be put in sleep mode and to quit out of Access when that event occurs.

    You will need to add a reference to the Microsoft WMI Scripting V#.# Library. The first code block is in a class module. The second code block is in a form.

    Class Module
    Code:
    Option Explicit
    
    'This is to allow the variable sink to raise events
    Dim WithEvents sink As SWbemSink
    
    Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet)
        'Event Type 4 is the code for entering sleep mode
        If objWbemObject.EventType = 4 Then
            Application.Quit
        End If
    End Sub
    
    Private Sub Class_Initialize()
        'Creates a new WMI object
        Dim services As SWbemServices
    
        'Creates the callback object
        Set sink = New SWbemSink
    
        'Gets the WMI service. The period in the string is used to reference the local computer.
        'You may use an IP address if you wish to query a remote computer.
        Set services = GetObject("winmgmts:\\.\root\cimv2")
    
        'Executes an asynchronous notification query
        services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent"
    End Sub
    Form
    Code:
    Option Compare Database
    Option Explicit
    Dim sinker As Class1
    
    Private Sub Form_Load()
        Set sinker = New Class1
    End Sub
    Sample Queries
    Query to return information about storage devices where the device is a local drive.
    Code:
    SELECT * 
    FROM Win32_LogicalDisk 
    WHERE DriveType = 3
    Query to return information about the CPU.
    Code:
    SELECT * 
    FROM Win32_Processor
    Code to create a process on a local/remote computer.
    Code:
    Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2:Win32_Process")
    objWMI.Create "C:\Windows\system32\mstsc.exe", Null, Null, intProcID
    Code to kill a process based on the process id.
    Code:
    Set objAppList = objWMI.ExecQuery("Select * from Win32_Process where ProcessID = '"& strID &"'",,48)
    For Each objApp In objAppList
       objApp.Terminate
    Next
    Query to ping a computer.
    Code:
    SELECT * 
    FROM Win32_PingStatus 
    WHERE Address = '" & sAddress & "'"
    Notification query for whenever a process is created. The WITHIN 1 is a polling interval.
    Code:
    SELECT * 
    FROM __InstanceCreationEvent 
    WITHIN 1 
    WHERE TargetInstance ISA 'Win32_Process'
    Notification query for whenever a network connection status changes. e.g. when a network share goes up or down.
    Code:
    SELECT * 
    FROM __InstanceModificationEvent 
    WITHIN 1 
    WHERE Targetinstance ISA 'Win32_NetworkConnection'
    Last edited by Rabbit; Feb 15 '13, 06:16 PM.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    Nothing much to say really, but just thanks for making this whole concept available.

    Just the introduction to the idea is something I feel will prove very useful :-)

    PS. I had another look through and noticed something that might prove a very helpful addition - A link to WMI Reference (Windows). Feel free to incorporate this into the OP if you feel it may be of benefit to your target audience.
    Last edited by NeoPa; Feb 15 '13, 06:09 PM. Reason: Added PS.

    Comment

    Working...