当我想远程启动进程时要使用的Windows类

时间:2011-06-17 13:25:58

标签: c# .net wmi remoting

我想使用c#和WMI在另一台计算机上远程启动进程。我做了一些初步研究,发现我最终必须使用一个过程类。 “Win32_Process”是第一个看起来很明显的东西,然而,它似乎仅限于代表本地进程。我可以使用哪些其他Windows进程类?

以下是使用Win32_ScheduledJob类时的代码:

   static public String RemoteConnect()
    {
        try
        {
            ConnectionOptions conn = new ConnectionOptions();
            conn.Username = @"JV";
            conn.Password = @"Nazpal6180";
            conn.EnablePrivileges = true;
            conn.Impersonation = System.Management.ImpersonationLevel.Impersonate;
            ManagementScope scope = new ManagementScope("\\\\phsd194-JV\\root\\cimv2", conn);
            //scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
            //scope.Options.EnablePrivileges = true;
            scope.Connect();

            ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");

            ObjectGetOptions objectGetOptions = new ObjectGetOptions();
            ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);

            object[] objectsIn = new object[7];
            objectsIn[0] = "calc.exe";
            objectsIn[1] = "********140000.000000+480";
            objectsIn[5] = true;
            object outParams = classInstance.InvokeMethod("Create", objectsIn);
            String response = "Creation of the process returned: " + outParams;

            return response;
        }
        catch (ManagementException err)
        {
            String response = "An error occurred while trying to execute the WMI method: " + err.Message;
            //Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
            return response;
        }
    }

3 个答案:

答案 0 :(得分:1)

当您在评论中指出时,Win32_Process.Create方法无法用于远程启动交互式流程,因此,解决方法是您可以将Win32_ScheduledJob类与Create method一起使用。

检查此示例应用程序,该应用程序在一分钟内启动远程计算机中的记事本(假设远程计算机的时间与本地计算机的时间相同,如果不是,则可以使用{{3}获取本地时间来自远程计算机的{}或Win32_LocalTime,然后转换为UTC)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace ConsoleApplication11
{
    class Program
    {

        private static string DateTimetoUTC(DateTime dateParam)
        {
            string buffer = dateParam.ToString("********HHmmss.ffffff");
            TimeSpan tickOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateParam);
            buffer += (tickOffset.Ticks >= 0) ? '+' : '-';
            buffer += (Math.Abs(tickOffset.Ticks) / System.TimeSpan.TicksPerMinute).ToString("d3");
            return buffer;
        }

        static void Main(string[] args)
        {
            try
            {
                ConnectionOptions conn = new ConnectionOptions();
                conn.Username = "theusername";
                conn.Password = "password";
                //connectoptions.Authority = "ntlmdomain:";
                conn.EnablePrivileges = true;
                ManagementScope scope = new ManagementScope(@"\\192.168.52.128\root\cimv2", conn);
                scope.Connect();
                Console.WriteLine("Connected");

                ObjectGetOptions objectGetOptions = new ObjectGetOptions();
                ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob");
                ManagementClass classInstance = new ManagementClass(scope, managementPath, objectGetOptions);

                ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
                inParams["Command"] = @"notepad.exe";
                //the itme must be in UTC format
                string StartTime = DateTimetoUTC(DateTime.Now.AddMinutes(1));
                Console.WriteLine(StartTime);
                inParams["StartTime"] = StartTime;

                ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);
                Console.WriteLine("JobId: " + outParams["JobId"]);
                Console.ReadKey();
            }
            catch(ManagementException err)
            {
                Console.WriteLine("An error occurred while trying to execute the WMI method: " + err.Message);
                Console.ReadKey();
            }
        }
    }
}

答案 1 :(得分:0)

我相信C#只有一个Process类。我之前用它来启动远程进程。

答案 2 :(得分:0)

我会选择服务器/客户端架构,服务器可以根据某种网络调用启动进程。