Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我想远程启动进程时要使用什么Windows类_C#_.net_Wmi_Remoting - Fatal编程技术网

C# 当我想远程启动进程时要使用什么Windows类

C# 当我想远程启动进程时要使用什么Windows类,c#,.net,wmi,remoting,C#,.net,Wmi,Remoting,我想使用c#和WMI在另一台计算机上远程启动进程。我做了一些初步的研究,发现我最终不得不使用processclass。“Win32_进程”似乎是第一个明显可以使用的东西,但是,它似乎仅限于表示本地进程。我还可以使用哪些其他Windows process类 下面是使用Win32_ScheduledJob类时的代码: static public String RemoteConnect() { try { ConnectionOp

我想使用c#和WMI在另一台计算机上远程启动进程。我做了一些初步的研究,发现我最终不得不使用processclass。“Win32_进程”似乎是第一个明显可以使用的东西,但是,它似乎仅限于表示本地进程。我还可以使用哪些其他Windows process类

下面是使用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#只是一个
过程
类。我以前使用过它来启动远程进程。

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

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

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


是的,但现在我想起来,这是一个非常不安全的网络。当我将其移植到生产站点时,我最终做的是在远程机器上创建一个网页,然后该网页在本地启动该进程。要使process类在远程机器上工作,必须在远程机器上启用远程注册表服务。抱歉,刚才看到了这一点。非常感谢。我试过用这个。任务被添加到了调度程序中,但是,当触发(时间)到来时,我没有看到记事本执行。它甚至不在任务管理器中……我还添加了一行:inParams[“InteractiveWithDesktop”]=true,但仍然没有启动GUI,任务管理器中也没有notepad.exe进程。计划的任务再次出现在任务计划程序库中。@Jonathan,是否检查远程计算机的时间?记住执行作业的日期/时间必须大于当前时间。是的,我做到了,我确保开始时间在当前时间之后。
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();
            }
        }
    }
}