Windows Management Instrumentation (WMI) : Lecture (20 Jan)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

Lecture (20 Jan)

Windows Management Instrumentation (WMI)


WMI allows developers to use a simple, consistent mechanism to query for information or configure settings on computers across an enterprise. The amount of information available through the WMI interface is amazinghardware settings, performance information, driver configuration, BIOS information, application settings, event log information, and much more.
using System; using System.Management; public class RemoteConnect { public static void Main() { // Build an options object for the remote connection // if you plan to connect to the remote // computer with a different user name // and password than the one you are currently using. // This example uses the default values. ConnectionOptions options = new ConnectionOptions(); // // // // // Make a connection to a remote computer. Replace the "FullComputerName" section of the string "\\\\FullComputerName\\root\\cimv2" with the full computer name or IP address of the remote computer.

// options.Username = ""; // options.Password = "";

ManagementScope scope = new ManagementScope( "\\\\osman\\root\\cimv2", options); scope.Connect(); //Query system for Operating System information ObjectQuery query = new ObjectQuery( "SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { // Display the remote computer information Console.WriteLine("Computer Name : {0}", m["csname"]);

Downloaded from: www.onspot.pk

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]); Console.WriteLine("Operating System: {0}", m["Caption"]); Console.WriteLine("Version: {0}", m["Version"]); Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]); } } }

ConnectionOptions Class Specifies all settings required to make a WMI connection.

ManagementScope Class Represents a scope (namespace) for management operations.

Connect

Connects this ManagementScope to the actual WMI scope

ObjectQuery Class Represents a management query that returns instances or classes.

ManagementObjectSearcher Class Retrieves a collection of management objects based on a specified query. This class is one of the more commonly used entry points to retrieving management information. For example, it can be used to enumerate all disk drives, network adapters, processes and many more management objects on a system, or to query for all network connections that are up, services that are paused, and so on. When instantiated, an instance of this class takes as input a WMI query represented in an ObjectQuery or its derivatives, and optionally a ManagementScope representing the WMI namespace to execute the query in. It can also take additional advanced options in an EnumerationOptions. When the Get()()() method on this object is invoked, the ManagementObjectSearcher executes the given query in the specified scope and returns a collection of management objects that match the query in a ManagementObjectCollection.

Downloaded from: www.onspot.pk

Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

ManagementObjectCollection Class Represents different collections of management objects retrieved through WMI. The objects in this collection are of ManagementBaseObject-derived types, including ManagementObject and ManagementClass. The collection can be the result of a WMI query executed through a ManagementObjectSearcher

EXAMPLES
select Name FROM Win32_ComputerSystem select Caption, Description, DeviceID, NumberOfFunctionKeys, Status from win32_keyboard select Caption, Manufacturer, Status from Win32_FloppyDrive

Downloaded from: www.onspot.pk

You might also like