我想打开进程pon远程计算机,此远程计算机在本地网络内部。
我尝试此命令,并且在远程计算机中什么也没有发生,与我连接的该用户具有管理员权限。
两台机器都运行Windows 7

static void Main(string[] args)
{
    try
    {
        //Assign the name of the process you want to kill on the remote machine
        string processName = "notepad.exe";

        //Assign the user name and password of the account to ConnectionOptions object
        //which have administrative privilege on the remote machine.
        ConnectionOptions connectoptions = new ConnectionOptions();
        connectoptions.Username = @"MyDomain\MyUser";
        connectoptions.Password = "12345678";

        //IP Address of the remote machine
        string ipAddress = "192.168.0.100";
        ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2", connectoptions);

        //Define the WMI query to be executed on the remote machine
        SelectQuery query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
        object[] methodArgs = { "notepad.exe", null, null, 0 };
        using (ManagementObjectSearcher searcher = new
                    ManagementObjectSearcher(scope, query))
        {
            foreach (ManagementObject process in searcher.Get())
            {
                //process.InvokeMethod("Terminate", null);
                process.InvokeMethod("Create", methodArgs);
            }
        }

        Console.ReadLine();

    }
    catch (Exception ex)
    {
        //Log exception in exception log.
        //Logger.WriteEntry(ex.StackTrace);
        Console.WriteLine(ex.StackTrace);

    }
}

最佳答案

您没有使用该代码打开进程,而是枚举了所有正在运行的名为"iexplore.exe"的进程并关闭它们。

我认为更简单,更好的方法是使用SysInternals PsExecTask Scheduler API

如果要使用WMI,则代码应如下所示:

object theProcessToRun = { "YourFileHere" };

ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

theClass.InvokeMethod("Create", theProcessToRun);


----------回复您的评论------------------

首先,您需要改变编码的态度和方式,并阅读要复制/粘贴的代码。

然后,您应该学习更多有关编程语言的知识。

不,我不会为您编写代码。我给你一个提示,指出正确的方向。现在轮到您进行开发了。玩得开心!!

关于c# - 使用WMI在远程计算机上执行过程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19630015/

10-13 06:22