Windows Management Instrumentation (WMI: windows管理規範)

來源:互聯網
上載者:User
 作者:SPARON         E-MAIL:ZKY@163.COM        MAN:ZHAOKEYONG@HOTMAIL.COM
 

首先來看一段碼:

ManagementScope Conn = new ManagementScope("\\\\" + textBox1.Text + "\\root\\cimv2", options);

Conn.Connect();

ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");

ManagementObjectSearcher query1 = new ManagementObjectSearcher(Conn, oq);

ManagementObjectCollection queryCollection1 = query1.Get();

foreach (ManagementObject mo in queryCollection1)

{

string[] ss = { "" };

//執行重啟操作

if (operationstr.Text == "重新啟動")

{

mo.InvokeMethod("Reboot", ss);

}

}

 

 

這是一段運程操作電腦的代碼,看了這段碼感覺如何,是不是感覺像在操作資料庫,其實這一切都得義於我們今天所要說的主題———WMI。

在經過了兩天的研究後終於對WMI有了一些瞭解,由於本人對WMI瞭解的也不很深入,所以本文只會在淺層面進解析,僅起到拋磚引玉的作用,還望多切入進行更深入的探討。

 

WMI是Windows 2K/XP管理系統的核心;對於其他的Win32作業系統,WMI是一個有用的外掛程式。WMI以CIMOM為基礎,CIMOM即公用資訊模型對象管理器(Common Information Model Object Manager),是一個描述作業系統構成單元的對象資料庫,為MMC和指令碼程式提供了一個訪問作業系統構成單元的公用介面。有了WMI,工具軟體和指令碼程式訪問作業系統的不同部分時不需要使用不同的API;相反,作業系統的不同部分都可以插入WMI,一所示(該圖來自MSDN),工具軟體和WMI可以方便地讀寫WMI。(摘自:用WMI實現Windows系統自動管理)

 

圖一

 

WMI工具軟體:Windows Management Instrumentation (WMI) SDK

通過Windows Management Instrumentation Extensions for Microsoft Visual Studio .NET 2003 Server Explorer工具可以使用VS2003輕鬆的對WMI進行編程。

 

Windows Management Instrumentation (WMI) Tools工具可以方便地查看可用的CIM和Win32類

 

好了,回到我們剛才的例子,我們來進行分析。

首先我們建立了ManagementScope的instance,表示使用指定選項初始化 ManagementScope 類的、表示指定範圍路徑的新執行個體。

Conn.Connect();不用多說,就是開啟對象串連。

接著,又構造了一個ObjectQuery對象,並傳入了一個字串,關鍵是在這個字串,它看起來像Trance-SQL語句,但是,它其實就是WMI了。如所示,我們代碼所處的位置在[win32 subsystem],把WMI比作SQL資料庫,這個Win32_OperatingSystem類就相當於資料庫中的表了。這樣理解就簡單了吧,不過實際理論要理解清楚。

好了,下面我們就要執行個體化查詢對象——ManagementObjectSearcher了。資料庫操作過吧,一樣的,傳遞個連線物件和查詢對象,就OK了。

ManagementObjectSearcher.Get()方法是調用指定的 WMI 查詢並返回結果集合。

然後是遞迴循還讀取返回結果並進行操作,要說的是InvokeMethod方法。

其方法原型為:

public Object InvokeMethod (

         string methodName,//要操作的方法名

         Object[] args//包含參數值的數組

)//該方法返回對象值

 

好了大體都差不多了,最後要說的是Win32_OperatingSystem類。

Win32_OperatingSystem類為原生API函數(用指令碼對WMI編程不能直接存取Win32 API)

具體方法有(摘自:MSDN Library for Visual Studio 2005)

Reboot

Class method that shuts down the computer system, and then restarts it.

SetDateTime

Class method that allows setting the computer date and time.

Shutdown

Class method that unloads programs and DLLs to the point where it is safe to turn off the computer.

Win32Shutdown

Class method that provides the full set of shutdown options supported by Windows operating systems.

 

下面我們看個MSDN上的例子

using System;

using System.Management;

public class InvokeMethod

{   

    public static void Main()

    {

        ManagementClass processClass = new ManagementClass("Win32_Process");

        object[] methodArgs = {"notepad.exe", null, null, 0};

        object result = processClass.InvokeMethod("Create", methodArgs);

        Console.WriteLine("Creation of process returned: " + result);

        Console.WriteLine("Process id: " + methodArgs[3]);

    }

}

 

 

最後,我來對我們的例子作個簡單的總結:通過WMI使得我們操作運程電腦可以像操作資料庫一樣簡單,需要的步驟有:

1. 建立連線物件;

2. 開啟連線物件;

3. 建立串連通道對象;

4. 用連線物件和串連通道物件建構查詢;

5. 擷取查詢結果;

6. 具體操作。

 作品:利用WMI控制遠端電腦


附:參考資料

《MSDN Library for Visual Studio 2005》

MSDN Libray : WMI 指令碼入門

用WMI實現Windows系統自動管理

WMI CIM Studio

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.