What is WMI?

Source: Internet
Author: User

What is WMI?

Windows Management Instrumentation (Wmi) is a scalable system management structure that uses a unified, standard-based, and scalable object-oriented interface. It provides standard methods for interacting with System Administrator information and basic WMI APIs. It is mainly used by system management application developers and system administrators to access and manage operating system information; it can be used to generate tools for organizing and managing system information, so that system administrators can monitor system activities more closely.

WMI provides a set of rich System Management Services built in Microsoft Windows operating systems, and provides comprehensive management functions in systems with a large number of applications, services and devices. It allows application developers to use a simple and consistent mechanism to query information on any computer in an enterprise or perform system configuration.

The amount of information that can be obtained through the WMI interface is astonishing, including hardware settings, status information, drive configuration, BIOS information, application settings, event record information, and others. WMI obtains information through a set of APIS, but it represents a function that obtains information through a simple, industrial standard object management mode. This makes it unnecessary for application developers to learn the details of every API in windows.

The. NET Framework SDK provides comprehensive support for WMI. the. NET Framework SDK provides a special namespace "system. Management" for Visual C # To operate WMI ". The namespace "system. Management" provides a large number of WMI-related classes, interfaces, and enumeration. Before Using WMI, you must add a reference to system. Management. dll in the project, and then declare

Using system. Management;

2. Use WMI to connect to a remote computer

The system administrator can query the status and information of the remote computer by the machine name (or IP address), user name, and password of the target machine, and execute some management tasks by using scripts. It is very convenient to use WMI to connect to a remote computer. You can refer to the following format:

Managementobjectsearcher query;
Managementobjectcollection querycollection;
System. Management. objectquery OQ;

String machinename = "192.168.1.100"; // ip address or machine name of the connected target machine
CO. Username = "name"; // user name required for connection
CO. Password = "password"; // Password required for connection
String connectstring = "select * From win32_pnpsigneddriver"; // query the string

System. Management. managementscope MS = new system. Management. managementscope ("//" + machinename + "// root // cimv2", CO );
OQ = new system. Management. objectquery (connectstring );
Query = new managementobjectsearcher (MS, OQ );
Querycollection = query. Get ();

Through the IP address, the user name and password establish a connection that can query all the information of the win32_pnpsigneddrvier class (for details about this class, please query msdn. Now we can remotely obtain information about all the PNP drivers on the target machine. Is it convenient? After obtaining all the information, we can use the following two methods to obtain the required attributes.

Foreach (managementobject Mo in querycollection)
{
// Traverse all attributes to obtain the values of all attributes
Propertydatacollection searcherproperties = Mo. properties;
Foreach (propertydata SP in searcherproperties)
{
Console. writeline ("name = {0,-20}, value = {1,-20}", sp. Name, sp. value );
}
}

3. Use WMI to connect to a local computer

It is also very convenient to use WMI to connect to the local computer. We only need to modify the above Code a little.

String machinename = "localhost ";
CO. Username = "";
CO. Password = "";

You can also use the query statement directly in a relatively simple format.

String connectstring = "select * From win32_pnpsigneddriver ";

Selectquery = new selectquery (connectstring );
Managementobjectsearcher searcher = new managementobjectsearcher (selectquery );

Foreach (managementobject Mo in searcher. Get ())
{
Propertydatacollection searcherproperties = Mo. properties;
Foreach (propertydata SP in searcherproperties)
{
Console. writeline (sp. Name + "" + sp. value );
}
}

4. query statement format

The above connection string connectstring actually has many formats. For WMI, WQL is used to query the required information. WQL inherits some SQL syntax. It does not mean that all SQL statements can be used in WQL. Common formats are as follows.

(1) All attributes of all objects in the query system
Connectstring = "select * From win32_pnpsigneddriver"

(2) query the hardwareid attributes of all objects in the class.
Connectstring = "select hardwareid from win32_pnpsigneddriver"

(3) query the hardwareid and driverversion attributes of all objects in the class.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver"

(4) The hardwareid and driverversion attributes of all objects in the query class, And the hardwareid of the object must end with ibm254d
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like '% ibm254d '"

(5) query the hardwareid and driverversion attributes of all objects in the class, and the hardwareid of the object must start with monitor.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor % '"

(6) query the hardwareid and driverversion attributes of all objects in the class, and the hardwareid of the object must start with monitor and end with ibm254d, and there is only one arbitrary character between them.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d '"

(7) The hardwareid and driverversion attributes of all objects in the query class, And the hardwareid of the object must start with monitor and end with ibm254d, and there must be only one arbitrary character between them. The infname must be oem18.inf.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d 'and infname = 'oem18. inf '"

(8) query the hardwareid and driverversion attributes of all objects in the class. The hardwareid of the object must start with monitor and end with ibm254d. There is only one character between them, or the infname must be oem18.inf.
Connectstring = "select hardwareid, driverversion from win32_pnpsigneddriver where hardwareid like 'Monitor _ ibm254d 'or infname = 'oem18. inf '"

4.5 common WMI list

// Hardware
Win32_processor, // CPU Processor
Win32_physicalmemory, // physical memory
Win32_keyboard, // keyboard
Win32_pointingdevice, // enter the device, including the mouse.
Win32_floppydrive, // floppy disk drive
Win32_diskdrive, // hard drive
Win32_cdromdrive, // Optical Drive
Win32_baseboard, // Motherboard
Win32_bios, // BIOS chip
Win32_parallelport, // parallel port
Win32_serialport, // serial port
Win32_serialportconfiguration, // serial port configuration
Win32_sounddevice, // multimedia settings, usually the sound card.
Win32_systemslot, // motherboard slot (ISA & PCI & AGP)
Win32_usbcontroller, // USB controller
Win32_networkadapter, // Network Adapter
Win32_networkadapterconfiguration, // network adapter settings
Win32_printer, // printer
Win32_printerconfiguration, // printer settings
Win32_printjob, // printer task
Win32_tcpipprinterport, // printer port
Win32_potsmodem, // Modem
Win32_potsmodemtoserialport, // Modem Port
Win32_desktopmonitor, // display
Win32_displayconfiguration, // graphics card
Win32_displaycontrollerconfiguration, // video card settings
Win32_videocontroller, // video card details.
Win32_videosettings, // Display Mode Supported by the video card.

// Operating System
Win32_timezone, // Time Zone
Win32_systemdriver, // driver
Win32_diskpartition, // disk partition
Win32_logicaldisk, // Logical Disk
Win32_logicaldisktopartition, // partition and start and end location of the Logical Disk.
Win32_logicalmemoryconfiguration, // logical memory configuration
Win32_pagefile, // system page file information
Win32_pagefilesetting, // page File Settings
Win32_bootconfiguration, // system startup configuration
Win32_computersystem, // brief Computer Information
Win32_operatingsystem, // operating system information
Win32_startupcommand, // the system automatically starts the program
Win32_service, // system-installed Service
Win32_group, // System Management Group
Win32_groupuser, // system group account
Win32_useraccount, // User Account
Win32_process, // system process
Win32_thread, // system thread
Win32_share, // share
Win32_networkclient, // installed network client
Win32_networkprotocol, // installed network protocol

 

5 References
WMI reference: http://msdn2.microsoft.com/en-us/library/aa394572.aspx
WMI tools: http://www.microsoft.com/downloads/details.aspx? Familyid = 6430f853-1120-48db-8cc5-f2abdc3ed314 & displaylang = en
WQL query: http://www.microsoft.com/china/technet/community/scriptcenter/topics/win2003/like.mspx#EIB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.