使用 WMI 在远程机器中执行进程 [英] Execute a process in a remote machine using WMI

查看:43
本文介绍了使用 WMI 在远程机器中执行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在远程机器上打开进程,这个远程机器在本地网络中.我尝试了这个命令,在远程机器上什么也没有发生,我连接的这个用户有管理员权限.两台机器都运行 Windows 7

I want to open process pon remote machine, this remote machine is inside local network. I try this command and in the remote machine nothing happen, this user that i connect with have administrators rights. Both machines running 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" 的所有正在运行的进程并关闭它们.

you are not opening a process with that code but you are enumerating all the running process named "iexplore.exe" and close them.

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

I think an easier, better way is to use SysInternals PsExec or the Task Scheduler API

如果您想使用 WMI,您的代码应如下所示:

If you want to use WMI your code should look like this:

object theProcessToRun = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);

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

----------In reply to your comment------------------

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

First of all you need to change your attitude and approach to coding and read the code that your are copy/pasting.

那你应该多学习一点编程语言.

Then you should study a little more about programming languages.

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

No I will not write the code for you. I gave you an hint to point to the right direction. now it is your turn to develop it. Have fun!!

这篇关于使用 WMI 在远程机器中执行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆