【问题标题】:What Windows Class to use when I want to start a process remotely当我想远程启动进程时使用什么 Windows 类
【发布时间】:2011-06-17 13:25:58
【问题描述】:

我想使用 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;
        }
    }

【问题讨论】:

    标签: c# .net wmi remoting


    【解决方案1】:

    当您指向 cmets 时,Win32_Process.Create 方法不能用于远程启动交互式进程,因此您可以将 Win32_ScheduledJob 类与 Create method 一起使用作为解决方法。

    检查这个示例应用程序,它在一分钟内启动远程机器中的记事本,(假设远程机器的时间与本地机器的时间相同,如果不是,您可以使用Win32_LocalTime 或Win32_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();
                }
            }
        }
    }
    

    【讨论】:

    • 抱歉,刚看到这个。非常感谢。我试过用这个。该任务已添加到调度程序中,但是,当触发器来临时(时间),我没有看到记事本执行。它甚至不在任务管理器中......
    • 我还添加了以下行:inParams["InteractWithDesktop"] = true 但仍然没有启动 GUI,并且任务管理器中没有 notepad.exe 进程。计划任务再次出现在任务计划程序库中。
    • @Jonathan,你检查远程机器的时间吗?记住用于执行作业的日期/时间必须大于当前时间。
    • 是的,我做到了,我确保开始时间在当前时间之后。
    【解决方案2】:

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

    【讨论】:

    • 是的,但是现在我考虑到它是在一个非常不安全的网络上。当我将它移植到生产站点时,我最终做的是在远程机器上创建一个网页,然后该页面在本地启动该过程。
    • 要让进程类在远程机器上工作,必须在远程机器上启用远程注册表服务。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 2018-05-09
      • 2018-08-16
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多