What Windows Class to use when I want to start a process remotely

风流意气都作罢 提交于 2019-11-29 13:04:54

As you point in your comments, the Win32_Process.Create method cannot be used to start an interactive process remotely, so as workaround you can use the Win32_ScheduledJob class with the Create method.

Check this sample app, which start the notepad in a remote machine in one minute , (Assuming which the time of the remote machine is the same of the local machine, if not you can get the local time using Win32_LocalTime or Win32_UtcTime from the remote machine and then convert to 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();
            }
        }
    }
}

I believe C# has just a Process class. I've used it for starting remote processes before.

I'd go for a server/client architecture where the server can start processes based on some kind of network call.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!