Sunday, 21 December 2014

WMI Explained and How to use with .NET

WMI Explained and How to use with .NET

WMI is not new but as old as windows. WMI full form is Windows Management Instrumentation, which is basically an interface to the Windows OS system settings, drivers ,hardware.... .You can use wmi to intrograte windows system and servers along with network . WMI use scripting language like VnScript,JScript(not javaScript) to interact with windows.
Later microsoft integrated WMI namespace available in .NET. So, we  can use WMI with c#,vb.net(all language which work on .net)
A .NET developer can use WMI to obtain information about drivers installed on the client machine, verify whether the system is licensed or not, check for hardware information of each and every deveice and a lot more.
WMI is a very powerfull tool, and once you know how to get what you need, it can be invaluable as a time saver. When developing Windows applications, developers often need information a system, either local or remote, that although commonplace, can be very tough to get. There is using the remote registry, but I myself do not allow remote registry access as do many network admins. WMI is usually wide open on networks, assuming you have the required rights to query it .

WMI Architecture

The purpose of WMI is to present a uniform interface to any local or remote applications or scripts that need to access management data from a computer system, network or application. Thanks to WMI, programs do not have to talk to a wide variety of operating system APIs which can be particularly inconvenient for scripting languages.
All WMI interfaces are based on the Component Object Model (COM), however it is possible to access WMI from .NET thanks to the COM Inter-Op mechanism, as our diagram shows.




The main parts of WMI are as follows:
Managed objects and providers: A WMI provider is a COM object that monitors one or more managed objects for WMI. Like a driver, a provider extracts WMI data from a managed object and is responsible for passing messages from WMI back to the managed object.
WMI infrastructure: As a Windows component WMI consists of the Windows Management service, which includes the WMI Core, and the WMI repository. The service acts as an intermediary between the providers and the repository. Only static data about objects, such as the classes defined by the providers, are stored in the repository.
Management applications and scripts: A management application queries management information either by calling the COM API for WMI or through the Scripting API for WMI.
Assume you have to Query a database, you will choose SQL to query it likewise  we have WQL to Query a windows system
If you know the provider classes and the fields available, then you can get the info very easily.  For instance, if you wanted to get a list of logical drives from a system you would use the following query:
Select * from Win32_LogicalDisk
You can, however, refine the search by using where clauses and getting specific "fields" in the query. The following query gets the amount of freespace, the size, and the name of all fixed disk drives:
Select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3
As you can see, constructing a simple WMI query is quite easy.  To get results, you need and interface, and in .Net it is provided by the System.Management namespace.  To make it work all you need is a query, and a little bit of code, just as if you were querying a database.
Note : while starting working with WMI make sure you set refrence to system.management.instrumentation. namespace

What Is WMI Provider?

WMI Provider is a software component that functions as a mediator between the CIM Object Manager and managed objects. By using the WMI APIs, providers supply the CIM Object Manager with data from managed objects, handle requests on behalf of management applications, and generate event notifications.

Microsoft WMI Code Creator

The Microsoft WMI Code Creator application  here. once downloaded and ran locally,you will see below screen
 
 “The WMI Code Creator tool allows you to generate VBScript, C#, and VB.NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI.”.I you will see below ,it generates code as per your choice
 
 CONTINUE....
Further readings http://msdn.microsoft.com/en-us/library/ms257353%28v=vs.80%29.aspx