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"]);
}
}
}