Understanding win's powerful management tool-WMI (SCRIPT)

Source: Internet
Author: User
Tags in domain

You may have heard of WMI, which is a Windows Management tool. You can see how powerful its functions are by name. It can manage all aspects of windows, from common file operations to hardware management, all can be implemented through WMI.

The syntax of WMI is very simple and almost identical for all namespaces and objects.

1. Connection

WMI can manage both local computers and administrator computers. Therefore, the first step to create WMI is to connect to the computer.

Dim k
K = "Microsoft" Give namespace
Set MyWMIObj = GetObject ("winmgmts: \. oot" & k ).

The above is my frequently-used code, which is very simple. The complete method is listed in MSDN:

On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "Microsoft" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)

The above method allows us to understand how to manage remote computers, so that we can write programs that can be centrally managed.


Ii. Enumeration

Each namespace has many objects, and each object can be processed as a set.
-------------------------------------
DNS

On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "MicrosoftDNS" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
The Set Domain = ProObj. ExecQuery ("Select * from MicrosoftDNS_Zone") method looks like the SQL syntax. Yes, but it does not have many functions, such as like.
For Each d in Domain
Response. write d. Name & "<br>"
Next

---------------------------------
IIS


On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "MicrosoftIISv2" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
Set IIS = ProObj. ExecQuery ("Select * from IIsWebService WHERE AnonymousUserName = Adminstrator") AnonymousUserName is an element in the site
For Each d in IIS
Response. write d. Name & "<br>"
Next


---------------------------------
Users and groups


On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "cimv2" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
Set us = ProObj. ExecQuery ("Select * from Win32_Account ")
For Each d in us
Response. write d. Name & "<br>"
Next

 


---------------------------------
Volumes and partitions


On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "cimv2" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
Set disk = ProObj. ExecQuery ("Select * from Win32_DiskPartition ")
For Each d in disk
Response. write d. Name & "<br>"
Next

 

The above lists some enumeration methods for objects of different types of namespaces. From the above method, we can see that the usage is the same.


3. Create


On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "cimv2" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
---------------------------------------------------------------------
Set o = ob1_miservice. Get ("Win32_Account ")
O. CreateInstanceFromPropertyData ***,***,***,.....
---------------------------------------------------------------------


The upper part of the code above is the same as that in section 2.

The following code is the object element in the created Code. For details, refer to MSDN.

4. Modify

On Error Resume Next
Computer = "MyPC"
User = "MyPCadministrator"
Pwd = "Password"
K = "cimv2" Give namespace
Set MyWMIObj = CreateObject ("WbemScripting. SWbemLocator ")
Set ProObj = MyWMIObj. ConnectServer (Computer, "root/" & k, user, pwd)
---------------------------------------------------------------------
Set colItems = obw.miservice. ExecQuery ("Select * from Win32_Account where Name = Administrator ")
For Each o In colItems
O. Modify ***,***,***,.....
Next
---------------------------------------------------------------------

The upper part of the code above is the same as that in section 2.

The following is the modified Code. When modifying the code, you must determine the uniqueness of the modification. Therefore, the where

* **,... Is the object element. When the element is modified, it is different from the element at the time of creation. For details, refer to the MSDN


At this point, the usage of WMI has been basically introduced. Through the above content, we can see that WMI is so simple and powerful. Of course, this is not the only thing about WMI. Due to my limited level, there are many things that are still unknown. We hope you will discuss them together.

Vi. Permissions

Before Using WMI, we should know that WMI can only be used by members of the Administrators group or authorized users.

From MS: authorize WMI users and Set permissions
_ One> 1. Enable WMI control.
2. In the console tree, right-click "WMI control" and click "properties ".
3. Click the Security tab.
4. Select the namespace that you want to grant the user or group access permissions to, and click "security ".
5. In the "Security" dialog box, click "add ".
6. In the "Select User, computer, or group" dialog box, enter the name of the object (user or group) You want to add in the "search range" box. Click Check name to verify the input, and then click OK ". You may have to change its location or use the "advanced" button to query objects. For more information, see help in this dialog box.
7. In the "Security" dialog box, under "Permissions", select to allow or deny permissions for new users or groups. Level description
The execution method allows you to run the method exported from the WMI category or example.


Write All | allow all read, write, and delete permissions on all WMI objects, categories, and examples.
Partial write | allows write access to static WMI objects.
Provider write | allow write access to the objects provided by the provider.
Enable account | allow access to WMI objects.
Remote enabling | allow remote access to the namespace.
Read Security | allow read-only access to WMI security information.
Edit Security | WMI security information can be read and written.
 

 

Note: As my English level is too low, it is very difficult to check the MS documentation. The above content may be incorrect.

I found that it is hard to understand the documentation in MS, so

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.