Using WMI to control remote computers in Visual C #

Source: Internet
Author: User
Not only can WMI get the computer data it wants, but it can also be used for remote control. Remote control of computers is not only the dream of hackers, also most network managers are eager to get, especially in modern networks, every network administrator faces the LAN, are composed of a large computer group, if the effective management of each computer network is particularly important. At present, network management software is usually to run the client daemon on the remote computer, run a server-side foreground control program on the local computer, and realize the remote control of the computer through the direct communication of these two programs. The disadvantage of this approach is very obvious, when the client shut down the background program, this remote management can not be achieved, so in order to overcome this shortcoming, remote control software client program is done very covert, so that users in the unconscious to run the program.

In fact, as a remote control software, WMI is a good choice, especially in the current, Windows 2000 has become the mainstream operating system, the use of WMI to write remote control software can omit the previous remote control software one of the most headaches-the distribution of client programs.

Copyright notice: Any access to Tenkine authorized website, reprint, please be sure to keep the author information and the following links
Pole Development C # column: http://dev.yesky.com/msdn/msdnc/
 
The remote control program that is described in this section enables users to reboot and shut down remote computers. It uses WMI, and the following is the concrete implementation steps of using WMI to control a remote computer in Visual C #.

1. Start visual Studio. Net, select the file, new, Project menu, and then set the project type to Visual C # project in the New Project dialog box, set the template to Windows application, and in the name Type "Use WMI to control remote computer" in the text box and enter "E:/vs in the text box in the location." NET project, and then click the OK button. So in "E:/vs." NET project directory, you create a new folder, "Using WMI to control a remote computer," which holds the project file, "Using WMI to control remote computers."

2. Perform the second to fourth steps in the "Get remote computer hard drive Information" project again.

3. Take visual Studio. NET Current window to the Form1.cs window, and from the Windows Forms Components tab in the Toolbox, drag the following components into the form and perform the appropriate actions:

Three TextBox component to enter a remote computer name (or IP address), a remote computer WMI operation username, and password.

A ComboBox component that selects the type of operation to be performed on a remote computer.

Four label components.

A button component, named Button1, after dragging into the form, double-clicking the component, the system automatically produces the processing code for its Click event in the Form1.cs file.

4. Adjust the values of each component's properties according to the data in table 06:

Component type Component Name Property Setting the results
Form Form1 Text Using WMI to control a remote computer
Form1 FormBorderStyle FixedSingle
Form1 MaximizeBox False
Label Label1 Text Machine name or IP address:
Label2 Text Super User name with WMI permissions:
Label3 Text Password:
Label4 Text Select control type:
Textbox TextBox1 Text ""
TextBox2 Text ""
TextBox3 PasswordChar *
TextBox3 Text ""
Button Button1 Text Perform
Button1 FlatStyle Flat
Table 06: Key attributes for each component in the "Use WMI Control remote computer" project form table of values

and adjust the position and order of the components in the form according to the placement and arrangement of the components in Figure 09:


Figure 09: Design interface for using WMI to control remote computers

5. Take visual Studio. NET switches to the editing window of the Form1.cs file and replaces the processing code for the Button1 click event in Form1.cs with the following code. The following code is useful for remote control of a remote computer based on the user's choice:

private void Button1_Click (object sender, System.EventArgs e)
{
Determining the scope of WMI operations
ConnectionOptions options = new ConnectionOptions ();
Set the user name for the WMI connection operation
Options. Username = TextBox2.Text;
Set the user's password
Options. Password = TextBox3.Text;
Try
{
Managementscope Conn = new Managementscope ("////" + TextBox1.Text + "//root//cimv2", options);
Conn.connect ();
Determining the contents of a WMI operation
ObjectQuery OQ = new ObjectQuery ("SELECT * from Win32_OperatingSystem");
ManagementObjectSearcher Query1 = new ManagementObjectSearcher (Conn, OQ);
Get WMI Action Content
Managementobjectcollection QueryCollection1 = Query1. Get ();
Perform the appropriate remote operation based on the user selection
foreach (ManagementObject mo in QueryCollection1)
{
string [] ss= {"};
Perform a reboot operation
if (Combobox1.text = "reboot")
{
Mo. InvokeMethod ("Reboot", SS);
} else
Perform a remote shutdown
if (Combobox1.text = "Remote Shutdown")
{
Mo. InvokeMethod ("Shutdown", SS);
}else
MessageBox.Show ("Select an incorrect action.) "," the error. ");
}
}
Error
catch (Exception ee)
{
MessageBox.Show ("Connection" + TextBox1.Text + "error, error message is:" + EE. message);
}
}

6. At this point, all of the work under "Using WMI to control a remote computer" is complete after the steps are completed correctly and saved. When you click the F5 shortcut, you are ready to run the program. In the program's computer name or IP address text box, enter the name or IP address of the remote computer you want to control. In the user name with WMI permissions and password text box, enter the account and password with WMI operations, and after selecting the control type for the remote computer in the Select control type combo box, click Execution button, the program is able to control the specified remote computer accordingly. Figure 10 is the running interface for "using WMI to control a remote computer."


Figure 10: Running interface using WMI to control remote computers

   Summary:

From the example above, it is obvious that WMI is a very useful thing, but since it is relatively new, there is no complete presentation at home or even abroad, so it's just a brief glance at the application of WMI, and hopefully the content of this article will help you understand and learn how to use Visual C # to invoke WMI.

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.