473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Windows Management Instrumentation (WMI) Tutorial

Rabbit
12,516 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. 'This is to allow the variable sink to raise events
  4. Dim WithEvents sink As SWbemSink
  5.  
  6. Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet)
  7.     'Event Type 4 is the code for entering sleep mode
  8.     If objWbemObject.EventType = 4 Then
  9.         Application.Quit
  10.     End If
  11. End Sub
  12.  
  13. Private Sub Class_Initialize()
  14.     'Creates a new WMI object
  15.     Dim services As SWbemServices
  16.  
  17.     'Creates the callback object
  18.     Set sink = New SWbemSink
  19.  
  20.     'Gets the WMI service. The period in the string is used to reference the local computer.
  21.     'You may use an IP address if you wish to query a remote computer.
  22.     Set services = GetObject("winmgmts:\\.\root\cimv2")
  23.  
  24.     'Executes an asynchronous notification query
  25.     services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent"
  26. End Sub
Form
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim sinker As Class1
  4.  
  5. Private Sub Form_Load()
  6.     Set sinker = New Class1
  7. End Sub
Sample Queries
Query to return information about storage devices where the device is a local drive.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM Win32_LogicalDisk 
  3. WHERE DriveType = 3
Query to return information about the CPU.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM Win32_Processor
Code to create a process on a local/remote computer.
Expand|Select|Wrap|Line Numbers
  1. Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2:Win32_Process")
  2. objWMI.Create "C:\Windows\system32\mstsc.exe", Null, Null, intProcID
Code to kill a process based on the process id.
Expand|Select|Wrap|Line Numbers
  1. Set objAppList = objWMI.ExecQuery("Select * from Win32_Process where ProcessID = '"& strID &"'",,48)
  2. For Each objApp In objAppList
  3.    objApp.Terminate
  4. Next
Query to ping a computer.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM Win32_PingStatus 
  3. WHERE Address = '" & sAddress & "'"
Notification query for whenever a process is created. The WITHIN 1 is a polling interval.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM __InstanceCreationEvent 
  3. WITHIN 1 
  4. WHERE TargetInstance ISA 'Win32_Process'
Notification query for whenever a network connection status changes. e.g. when a network share goes up or down.
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM __InstanceModificationEvent 
  3. WITHIN 1 
  4. WHERE Targetinstance ISA 'Win32_NetworkConnection'
Sep 4 '12 #1
1 32657
NeoPa
32,556 Expert Mod 16PB
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.
Feb 15 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Colin Brown | last post by:
Recently I was looking for remote management tools and came across "Windows Management Instrumentation". There is a python interface available: http://tgolden.sc.sabren.com/python/wmi.html I...
3
by: David Shen | last post by:
Hi, Here is text copy/paste from the WMI SDK reference Use the Win32_OperatingSystem class and the Win32Shutdown method. You must include the RemoteShutdown privilege when connecting to WMI....
1
by: sw | last post by:
i couldn't find a good newsgroup for this, sorry it this is a bad place... my question: i would like to install wmi core library when someone with win98se is installing my application. i...
1
by: Bill | last post by:
As I have looked all over, even MSDN, I cannot find much on iis management using wmi in c#. Does anyone have sample code or any documentation. If so please email to me bduffy@no.spam@4duffy.com...
3
by: cmrchs | last post by:
Hi, using the Windows management Instrumentation interface (WMI) do I get illogical results : the following works : ManagementObject account = new ManagementObject...
5
by: Primera | last post by:
I use the following method to stop the WMI service: string serviceName = "Windows Management Instrumentation" public static void stopService(string serviceName) { ServiceController services =...
0
by: Christian Havel | last post by:
Hi, I retrieve the following calling list when a exception appears in ReleaseComObject. mscorlib.dll!System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object o = <Nicht...
0
by: Samuel Shulman | last post by:
Using the System.Management.Instrumentation Namespace, can I enable Disable devices? Thank you, Sam
0
by: tirumalab | last post by:
I need help regarding Registry changes using WMI. how to use this namespace "stdregprov".... kindly suggest me ..plz.
3
by: =?Utf-8?B?QnJpYW4gTmljaG9sc29u?= | last post by:
I'm building a test web application that, when run, will launch calc.exe on my computer. It seems to work as it appears in Windows Task Manager; however, the process doesn't actually pop up...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.