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

本文关键字:Windows 程启动 启动 进程 | 更新日期: 2023-09-27 17:59:19

我想使用c#和WMI在另一台计算机上远程启动进程。我做了一些初步的研究,发现我最终必须使用processclass。"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;
        }
    }

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

正如您在评论中指出的,Win32_Process.Create方法不能用于远程启动交互式进程,因此作为解决方法,您可以将Win32_ScheduledJob类与Create method一起使用。

检查这个示例应用程序,它可以在一分钟内启动远程机器中的记事本(假设远程机器的时间与本地机器的时间相同,如果不是,则可以使用Win32_LocalTimeWin32_UtcTime从远程机器获取本地时间,然后转换为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();
            }
        }
    }
}

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

我会选择服务器/客户端架构,在该架构中,服务器可以基于某种网络调用启动进程。