Запуск процесса на удаленной машине

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Text;
namespace RemoteMachine
{
public class Remote
{
public static int StartProcess(string Command, string MachineName, string UserName, string Password)
{
System.Management.ConnectionOptions options = new System.Management.ConnectionOptions();
options.Username = UserName;
options.Password = Password;
System.Management.ManagementPath path = new System.Management.ManagementPath(@"\\" + MachineName + @"\root\cimv2:Win32_Process");
System.Management.ManagementScope scope = new System.Management.ManagementScope(path, options);
scope.Connect();
System.Management.ObjectGetOptions opt = new System.Management.ObjectGetOptions();
System.Management.ManagementClass classInstance = new System.Management.ManagementClass(scope, path, opt);
System.Management.ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
inParams["CommandLine"] = Command;
System.Management.ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);
return Convert.ToInt32(outParams["processId"]);
}
}
}