Wmi ( windows management instrument ) 研究

一、初始化 (1)加入參考System.manegement (2) using System.management

二、首先在連線的部分:

(1)本機:new ManagementClass(path)    ex: path = “Win32_Process”

(2)遠端:new ManagementClass(Scope,Path, null)

其中Scope=new managementScope(@\\192.168.1.1\root\cimv2,options)

其中options=new connectionOptions()

  options.Username  .password是遠端登入帳密

三、範例:

        (1)查本機所有進程

            ManagementObjectSearcher mos = new ManagementObjectSearcher(@"\\.\root\CIMV2", "SELECT * FROM Win32_Process");

       ManagementObjectCollection moc = mos.Get();

foreach (ManagementObject mo in moc) {

                     MessageBox.Show(mo.GetText(new TextFormat()));

}

     (2)查遠端所有進程

           ManagementScope MS_Conn;

System.Management.ConnectionOptions options;

options = new ConnectionOptions();

options.Username = 帳號;

options.Password = 密碼;

MS_Conn = new ManagementScope(@"\\" + ip + @"\root\cimv2", options);

         MS_Conn.Connect();

 ManagementObjectSearcher mos = new ManagementObjectSearcher(MS_Conn, new ObjectQuery("SELECT * FROM Win32_Process"));

         ManagementObjectCollection moc = mos.Get();

         foreach (ManagementObject mo in moc) {}

*注意幾個地方  若登入的使用者號為本機 則 \\帳號   若有網域的就直接打帳號

*另外  遠端使用Win32_Process類別的Create來執行程式時,會背景執行

*本機程式若要背景執行相關執行設定請參考Win32_ProcessStartup類別 設定好後可放入Win32_Process開檔時使用 InvokeMethod(Create,method參數,win32_processstartup)

 

 

相關範例請看下面:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Management;

using System.Diagnostics;

 

namespace FrmWMISample {

     public partial class Form1 : Form {

           ManagementScope MS_Conn;

           System.Management.ConnectionOptions options;

           public Form1() {

                InitializeComponent();

          }

 

           private void Form1_Load(object sender, EventArgs e) {

                comboBox1.SelectedIndex = 0;

           }

 

           private void button1_Click(object sender, EventArgs e) {

               

               options = new ConnectionOptions();

                options.Username = textBox2.Text;

                options.Password = textBox3.Text;

                options.Authentication = AuthenticationLevel.PacketPrivacy;

                options.Impersonation = ImpersonationLevel.Impersonate;

                options.EnablePrivileges = true;

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

                MS_Conn.Connect();

                if (comboBox1.Text == "重開機") { reboot(); }

                else if (comboBox1.Text == "開檔案") { create(textBox4.Text); }

                else if (comboBox1.Text == "查本機") { query(textBox4.Text); }

                else if (comboBox1.Text == "查遠端") { query1(textBox4.Text); }

                else if (comboBox1.Text == "關檔案") { close(textBox4.Text); }

                else if (comboBox1.Text == "檔全關") { closeall(); }

           }

           private void reboot() {

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

                ManagementObjectSearcher mos1 = new ManagementObjectSearcher(MS_Conn, oq);

                ManagementObjectCollection moc1 = mos1.Get();

                foreach (ManagementObject mo in moc1) {

                     mo.InvokeMethod("Reboot", null);

                     MessageBox.Show(string.Format("電腦: {0} 已重新開機...", textBox1.Text));

                }

           }

           //----------------------------------------

           private void create(string n) {

                ManagementClass mc2 = new ManagementClass(MS_Conn, new ManagementPath("Win32_Process"), null);

                ManagementClass mc = new ManagementClass(MS_Conn, new ManagementPath("Win32_ProcessStartup"), null);

                //把屬性[ShowWindow]設為0就會背景跑  1就會顯示了!gogogo

                mc.Properties["ShowWindow"].Value = 1;

 

                ManagementBaseObject mbo = mc2.GetMethodParameters("Create");

                mbo["CommandLine"] = n;

                mbo["ProcessStartupInformation"] = mc;

                mc2.InvokeMethod("Create", mbo, null);

           }

           //----------------------------------------

           private void query(string n) {

                ManagementObjectSearcher mos = new ManagementObjectSearcher(@"\\.\root\CIMV2", "SELECT * FROM Win32_Process");

                ManagementObjectCollection moc = mos.Get();

                ManagementBaseObject mbo= new ManagementClass("Win32_Process").GetMethodParameters("Create");

                mbo["CommandLine"]=n;

                foreach (ManagementObject mo in moc) {

                     MessageBox.Show(mo.GetText(new TextFormat()));

                     //mo.InvokeMethod(new ManagementOperationObserver(), "GetOwner",null);

                     //MessageBox.Show(mo.InvokeMethod("GetOwner",null));

                     //MessageBox.Show(mo["Caption"].ToString());

                     //if (mo["Caption"].ToString() == n) {

                     //   mo.InvokeMethod("Terminate",null);

                     //}

                }

                //ManagementClass mc = new ManagementClass("Win32_Process");

           }

           private void query1(string n) {

                ManagementObjectSearcher mos = new ManagementObjectSearcher(MS_Conn, new ObjectQuery("SELECT * FROM Win32_Process"));

                ManagementObjectCollection moc = mos.Get();

                foreach (ManagementObject mo in moc) {

                     comboBox2.Items.Add( mo["Name"].ToString());

                }

           }

           private void close(string n) {

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

                ManagementObjectSearcher mos = new ManagementObjectSearcher(MS_Conn,oq);

                ManagementObjectCollection moc = mos.Get();

                ManagementBaseObject mbo = new ManagementClass("Win32_Process").GetMethodParameters("Create");

                mbo["CommandLine"] = n;

                foreach (ManagementObject mo in moc) {

                     //MessageBox.Show(mo["Caption"].ToString());

                     if (mo["Caption"].ToString() == n) {

                           mo.InvokeMethod("Terminate",null);

                     }

                }

           }

           private void closeall() {

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

                ManagementObjectSearcher mos = new ManagementObjectSearcher(MS_Conn, oq);

                ManagementObjectCollection moc = mos.Get();

                foreach (ManagementObject mo in moc) {

                     //MessageBox.Show(mo["Caption"].ToString());

                     if (mo["Caption"].ToString() != "explore.exe") {

                           mo.InvokeMethod("Terminate", null);

                     }

                }

           }

 

           private void button2_Click(object sender, EventArgs e) {

                textBox1.Text = "192.168.32.152";

                textBox2.Text = "led";

                textBox3.Text = "1111";

                comboBox1.Text = "開檔案";

                textBox4.Text = "cmd.exe";

           }

 

           private void button3_Click(object sender, EventArgs e) {

                ManagementObject mo = new ManagementObject(@"\\.\root\CIMV2\Win32_Process");

                mo.Options.UseAmendedQualifiers = true;

                mo.GetPropertyValue("Name");

           }

 

           private void button4_Click(object sender, EventArgs e) {

                ManagementClass processClass = new ManagementClass("Win32_Process");

 

                ManagementObjectCollection classObjects;

                classObjects = processClass.GetInstances();

 

                foreach (ManagementObject classObject in classObjects) {

                     MessageBox.Show(classObject.GetPropertyValue("Name").ToString());

 

                }

           }

 

           private void button5_Click(object sender, EventArgs e) {

                // Get the WMI class

                ManagementClass processClass =

                     new ManagementClass("Win32_Process");

                processClass.Options.UseAmendedQualifiers = true;

 

                // Get the properties in the class

                PropertyDataCollection properties =

                     processClass.Properties;

 

                // display the properties

                string temp="Win32_Process Property Names: ";

                foreach (PropertyData property in properties) {

                     temp+= property.Name;

                     MessageBox.Show(temp);

 

                     foreach (QualifierData q in property.Qualifiers) {

                           if (q.Name.Equals("Description")) {

                                MessageBox.Show(

                                     processClass.GetPropertyQualifierValue(

                                     property.Name, q.Name).ToString());

                           }

 

                     }

                     Console.WriteLine();

 

                }

           }

 

           private void button6_Click(object sender, EventArgs e) {

 

 

        // Get the WMI class

        ManagementClass processClass =

            new ManagementClass("Win32_Process");

 

        // Get the system properties for the class

        PropertyDataCollection properties =

            processClass.SystemProperties;

 

        // display the properties

        foreach (PropertyData p in properties)

        {

                MessageBox.Show(p.Name.ToString() + " " + p.Value.ToString());

        }

  

           }

 

           private void button7_Click(object sender, EventArgs e) {

    

 

        // Get the object on which the

        // method will be invoked

        ManagementClass processClass =

            new ManagementClass("Win32_Process");

 

        // Create an array containing all

        // arguments for the method

        object[] methodArgs =

            {"cmd.exe", null, null, 0};

 

        //Execute the method

        object result =

            processClass.InvokeMethod(

            "Create", methodArgs);

 

           }

 

           private void button8_Click(object sender, EventArgs e) {

                options = new ConnectionOptions();

                options.Username = textBox2.Text;

                options.Password = textBox3.Text;

                //options.Authentication = AuthenticationLevel.Default;

                options.Impersonation = ImpersonationLevel.Impersonate;

                options.EnablePrivileges = true;

                //options.Timeout = TimeSpan.MaxValue;

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

                ms.Connect();

                ManagementClass processClass = new ManagementClass(ms,new ManagementPath("Win32_Process"),null);

                //ManagementClass startup=new ManagementClass("Win32_ProcessStartup");

               

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

                object result =

                     processClass.InvokeMethod(

                     "Create", methodArgs);

           }

 

           private void button9_Click(object sender, EventArgs e) {

                Form2 f2 = new Form2();

                this.Visible = false;

                f2.ShowDialog();

                if (f2.IsDisposed) this.Dispose();

                else this.Visible = true;

           }

 

           private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) {

                ManagementObjectSearcher mos = new ManagementObjectSearcher(MS_Conn, new ObjectQuery("SELECT * FROM Win32_Process where Name='"+comboBox2.Text+"'"));

                ManagementObjectCollection moc = mos.Get();

                foreach (ManagementObject mo in moc) {

                     MessageBox.Show(mo.GetText(new TextFormat()));

                }

           }

 

           private void button10_Click(object sender, EventArgs e) {

                ManagementClass mc = new ManagementClass("Win32_ProcessStartup");

                //把屬性[ShowWindow]設為0就會背景跑  1就會顯示了!gogogo

                MessageBox.Show((mc.Properties["ShowWindow"].Value=1).ToString());

 

                ManagementClass mc2 = new ManagementClass("Win32_Process");

                ManagementBaseObject mbo=mc2.GetMethodParameters("Create");

                mbo["CommandLine"]=textBox4.Text;

                mbo["ProcessStartupInformation"]=mc;

                mc2.InvokeMethod("Create", mbo, null);

           }

 

           private void button11_Click(object sender, EventArgs e) {

                ManagementObjectSearcher mos = new ManagementObjectSearcher(@"\\.\root\directory\LDAP", "SELECT * FROM ds_user");

                ManagementObjectCollection moc = mos.Get();

                //foreach (Process p in Process.GetProcessesByName(textBox4.Text)) {

                foreach (ManagementObject mo in moc) {

                     //comboBox2.Items.Add(mo.);

                }

                //}

           }

 

           private void button12_Click(object sender, EventArgs e) {

                options = new ConnectionOptions();

                options.Username = "\\mis.admin";

                options.Password = "superkey";

                //options.Authentication = AuthenticationLevel.Default;

                options.Impersonation = ImpersonationLevel.Impersonate;

                options.EnablePrivileges = true;

                //options.

                //options.Timeout = TimeSpan.MaxValue;

                ManagementScope ms = new ManagementScope(@"\\192.168.32.152\root\cimv2", options);

                ms.Connect();

           }

 

     }

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小新 的頭像
    小新

    伯爵筆記 - 奧西諾inNow

    小新 發表在 痞客邦 留言(0) 人氣()