Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

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

I want to use c# and WMI to start a process remotely in another computer. I've made some initial research and found out that i ultimately have to use a processclass. The "Win32_Process" was the first thing that seemed obvious to be used, however, it seems it is limited to represent only local processes. What other Windows process classes can I use?

Here is what the code when using Win32_ScheduledJob class:

   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;
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
ootcimv2", 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();
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...