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
Expand|Select|Wrap|Line Numbers
- 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
Expand|Select|Wrap|Line Numbers
- Option Compare Database
- Option Explicit
- Dim sinker As Class1
- Private Sub Form_Load()
- Set sinker = New Class1
- End Sub
Query to return information about storage devices where the device is a local drive.
Expand|Select|Wrap|Line Numbers
- SELECT *
- FROM Win32_LogicalDisk
- WHERE DriveType = 3
Expand|Select|Wrap|Line Numbers
- SELECT *
- FROM Win32_Processor
Expand|Select|Wrap|Line Numbers
- Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2:Win32_Process")
- objWMI.Create "C:\Windows\system32\mstsc.exe", Null, Null, intProcID
Expand|Select|Wrap|Line Numbers
- Set objAppList = objWMI.ExecQuery("Select * from Win32_Process where ProcessID = '"& strID &"'",,48)
- For Each objApp In objAppList
- objApp.Terminate
- Next
Expand|Select|Wrap|Line Numbers
- SELECT *
- FROM Win32_PingStatus
- WHERE Address = '" & sAddress & "'"
Expand|Select|Wrap|Line Numbers
- SELECT *
- FROM __InstanceCreationEvent
- WITHIN 1
- WHERE TargetInstance ISA 'Win32_Process'
Expand|Select|Wrap|Line Numbers
- SELECT *
- FROM __InstanceModificationEvent
- WITHIN 1
- WHERE Targetinstance ISA 'Win32_NetworkConnection'