Wmi C # remotely start the exe file with an interface

Source: Internet
Author: User

Due to business needs, you need to batch remote servers to start the corresponding exe files. At first, I only started the file in the process, but it cannot be displayed on the interface. By asking for O & M, I can start the program with the interface through the task plan. Note that the Remote Desktop must be enabled on the computer, and pay attention to the firewall, I directly shut down the fire wall. The Internet says that the exception of port 135 is also acceptable and there is no test. Below is a demo of my test.

Go directly to the Code:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Management;
Using System. Management. Instrumentation;


Namespace test1
{
Class windowsWmi
{

Public void exStartCommand ()
{

// ConnectionOptions specifies the settings required to generate the wmi connection
String userName = "userName ";
String password = "password ";
ConnectionOptions connOption = new ConnectionOptions ();
ConnOption. Username = userName;
ConnOption. Password = password;
// ManagementPath encapsulates the path for generating and analyzing wmi objects
ManagementPath mngPath = new ManagementPath (@ "\" + "ip address" + @ "\ root \ cimv2: Win32_Process ");
ManagementScope scope = new ManagementScope (mngPath, connOption );
Scope. Connect ();
// The ObjectGetOptions class specifies the options used to obtain management objects.
ObjectGetOptions objOption = new ObjectGetOptions ();
// ManagementClass indicates the Common Information Model (CIM) management class. Members of this class can use specific WMI class paths to access WMI data.
ManagementClass classInstance = new ManagementClass (scope, mngPath, objOption );

ManagementBaseObject inParams = classInstance. GetMethodParameters ("Create ");

// Fill in input parameter values
// InParams ["CommandLine"] = @ "D: \ Program \ UserAnts20120530 \ UserAnts20120530 \ UserAnts3 \ TaskWorker.exe"; // only processes can be started
InParams ["CommandLine"] = "schtasks/run/tn \" Start03 \ ""; // Start03 is the name of the task scheduler. You need to create a scheduler task to start the exe.

// Method Options
InvokeMethodOptions methodOptions = new InvokeMethodOptions (null, System. TimeSpan. MaxValue );

// Execute the method
ManagementBaseObject outParams = classInstance. InvokeMethod ("Create", inParams, methodOptions );
// Console. WriteLine ("Creation of calculator process returned:" + outParams ["returnValue"]);
// Console. WriteLine ("Process ID:" + outParams ["processId"]);

}
}
}

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.