Use WMI for Automatic windows management

Source: Internet
Author: User

Compared with the previous operating system, Windows 2 k/XP has better manageability. For example, it supports remote management in Windows 2000 Server Terminal Service, Microsoft Management Console (MMC), and WMI. WMI is short for Windows Management Instrumentation. WMI has many purposes. One of them is to automate management through Script Programming.

I. Why script?




Looking back at the success of Windows NT in 1990s, one of the reasons is that the operating system is relatively easy to use, anyone who can use Windows 3.1 can manage a simple NT Network (which sounds incredible today, but the fact is that nt 3.1 is much less functional than Windows 2 k/XP, so it is much simpler ). Just look at the control panel of NT, and there is a general impression of what the operating system can do.

With the development of the operating system, the original friendly interface became more friendly. In Windows 2 k/XP, almost every operation process has a wizard, and every operating system-level object has a graphical property page; different operation options guide you to the final task completion dialog box. MMC allows you to insert commonly used tools (or even third-party tools) into a custom tool set.

However, the user-friendly GUI is too cumbersome for advanced users. In addition, although some tasks can also be executed on the command line, the command line does not have the full functionality as a graphical tool. To automate operation tasks in Windows 2 k/XP, a good way is to directly access the management interface accessed by graphical tools through a script program. WMI allows you to use a unified interface to access almost any part of the operating system using a scripting language. Of course, there are some restrictions on WMI programming using scripts, for example, you cannot directly access the Win32 API.

2. What is WMI?

WMI is the core of the Windows 2 k/XP management system. WMI is a useful plug-in for other Win32 operating systems. WMI is based on cimom. cimom is the Common Information Model object manager. It is an object database that describes the components of an operating system, provides a public interface for MMC and script programs to access the operating system components. With WMI, different APIs are not required when tool software and script programs access different parts of the operating system. On the contrary, WMI can be inserted into different parts of the operating system, as shown in (this figure is from msdn), the tool software and WMI can easily read and write WMI.

Figure 1

Both Windows 2 k/XP and Windows 98 support WMI. If Service Pack 4 or later is added to NT 4.0 and Windows 95, NT 4.0 and Win95 also support WMI. Therefore, when WMI is used for remote management, it is not necessary to use Windows 2 k/XP (of course, if the WMI script runs on a Win9x machine without a performance monitor, you cannot query windows 2 k/XP performance monitors on a remote Win9x system.

As mentioned above, WMI allows access to multiple operating system components through a public interface, so it is not necessary to treat various underlying interfaces or so-called "providers" separately ". WMI can be used to efficiently manage remote and local computers. In contrast, not all windows 2 k/XP command line tools support remote running.

WMI is an implementation of the WBEM model. WBEM is Web-Based Enterprise Management, or Web-Based Enterprise Management. WBEM is created by DMTF (Distributed Management Task Force, Distributed Management Task Group) with the help of many vendors, including Compaq, sun, and Microsoft. The goal of WBEM is to develop a standard interface set for managing the enterprise environment. The most important part of the WBEM model is its data model (or method for describing and defining objects), encoding specification (encoding specification), and data transmission mode between the client and the server.

The data model of WBEM is CIM (Common Information Model, Common Information Model ). CIM is a standard naming system (or named mode) used to name physical and logical units of a computer, such as a logical partition of a hard disk and an instance of a running application, or a cable.

CIM is an object-oriented model that is described using a set of object-oriented terms. CIM includes class, which is the template of the managed unit. An instance of a class is called an object. An object represents a specific unit of the underlying system. A namespace is a collection of classes. Each namespace is oriented to a specific management domain. Class includes property and method ).

CIM is divided into three layers. The first layer is the core model. The class definitions contained in this layer are common for all management fields. The second layer is the common model. The class definition contained in this layer is public for specific management fields, but it is irrelevant to the specific operating system and system design. The third layer is the extension model, which contains class definitions related to specific operating systems or technologies.

WMI is an extended model for Win32 systems derived from Microsoft extended CIM 2.0. The format of referencing WMI classes and properties is "extended prefix _ class name. "property name", for example, win32_computersystem.name. Win32 indicates the prefix of the WMI extension class in the cimv2 namespace in CIM mode, computersystem indicates the class, and name indicates the attribute.

A large part of WMI script writing involves reading and setting attribute values. At present, the methods provided by WMI are still very limited, but over time, I believe that WMI and CIM will provide more and more methods.

Iii. WMI software development kit

The WMI software development kit (SDK) allows you to conveniently view available CIM and Win32 classes. Wmi sdk can be from http://msdn.microsoft.com/downloads/default.asp? Url =/code/sample. asp? Url =/msdn-files/027/001/566/msdncompositedoc. xml download, there are more than 8 m, can be said not small.

Wmi sdk requires that the operating system be windows 2 k/XP or NT 4.0 SP4 or later. Although WMI support software can be installed on Win9x, the SDK cannot run on Win9x. In addition, to support SDK ActiveX controls, the SDK also requires IE 5.0 or later. The SDK has the following minimum requirements for machine performance: Pentium processor, 32 mb ram, 40 MB disk space, and display devices with at least 800x600 and 256 colors. For machines running Windows 2 k/XP, these requirements should not be a problem. Complete. Winmgmt.exe is in the WMI home directory of % SystemRoot %/system32/WBEM.

The SDK must be installed by members of the Administrator group. The installation process is simple. Run the wmisdk.exe startup Wizard to specify the target directory for installation (default:/program files/Wmi ). Select the optional components to be installed (all components except SNMP support are installed by default), and click Finish. You do not need to restart the SDK. After the installation is complete, a wmi sdk group will be added to the "Start/Program" menu.

Click wmi cim studio in the wmi sdk program group. CIM studio prompts the connection namespace and displays the default connection namespace root/cimv2. Just confirm. If you log on to Windows as administrator, click "OK" again to log on as the current identity. If you log on to Windows as another identity, use "Administrator" to log on.

Now, suppose we want to find an object on the current machine: C: Drive. We do not know the specific name of C: Drive in CIM or WMI. It is too troublesome to browse hundreds of Classes listed in CIM studio. What should we do? You can use the find button (for the telescope on the top left, see Figure 3 ). Figure 2 shows the search for class dialog box displayed after clicking the find button. Here, enter the words that should be included in the C: Drive class name, and click Go! Button. Because we are looking for a named disk partition, and we know that Windows calls this partition Logical Disk or logical drive, the search keyword here can be logical. Of course, the search keyword can also be disk, but there will be a large number of search results.

Figure 2

Figure 2 shows the logical result of the search keyword. Select win32_logicaldisk and click OK. The window shown in Figure 3 appears. (why not select cim_logicaldisk? As mentioned above, all objects managed by WMI have Win32 prefixes. If you select cim_logicaldisk and then want to display its instance, you cannot see any specific information about the available logical drive. You can only see win32_logicaldisk entries for each available logical drive ). The win32_logicaldisk class attribute is displayed on the right of the window. We can see that the attribute value is empty because we are viewing a class, not a specific instance of the class. To display the win32_logicaldisk instance, click the instances button on the right (fourth in the right ).

Figure 3

After you click the instances button, the window displays the instances of all logical drives on the current machine, including network logical drives. Click the instance whose device ID is "C:" To display the result of Figure 4. The right pane contains the attributes and methods of the current instance. The name of the Current Logical drive is displayed above the right pane.

Figure 4

You can use scripts to modify these attributes or call these methods. If you are not clear about the meaning of an attribute, you only need to select the win32_logialdisk class or win32_logicaldisk.deviceid = "C:" instance, and then click the Help button. The attributes and methods of most objects are described in detail.

Iv. Preliminary Script Programming

We used the SDK to view the win32_logicaldisk class and its attributes. Next we will see how to access this information in the script. If Microsoft Windows 2000 resource kit is installed on your system, a listfreespace. vbs script is generated by default in the/program files/resource kit folder. This script queries each object of the win32_logicaldisk class and extracts a set of attribute values: DeviceID, that is, the drive identifier; freespace, the number of bytes of the drive free space. Assume that the of a machine is a floppy disk drive, D is a CD-ROM drive, and the output class of listfreespace. vbs is as follows:

The following shows the code of the getfree. vbs script program, which uses less code to obtain similar information. When getfree. vbs is started, you must specify the drive identifier. getfree. vbs displays the drive free space and file system type.

In the Windows command line window, Run "wscript getfree. vbs C:" To display the result of Class 5. If "cscript getfree. vbs C:" is executed, the message is displayed as a character.

Figure 5

Another example is that the following VBScript prompts you to enter the name of the remote machine and disable the specified remote machine:

Of course, if you only have the knowledge in this article, you cannot be a master of WMI Script Programming. But now you know how to use the SDK to query information and how to use scripts to access object attributes and methods. Continue to work!

Related Article

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.