C# 使用WMI在远程计算机中执行进程

C# 使用WMI在远程计算机中执行进程,c#,process,wmi,wmi-query,C#,Process,Wmi,Wmi Query,我想打开进程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 = "notepa

我想打开进程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”
的所有正在运行的进程并将其关闭

我认为更简单、更好的方法是使用

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

object theProcessToRun = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);
----------回应你的评论------------------

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

然后你应该多学习一些编程语言


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

这是我之前使用vbs脚本为公司编写的脚本。可以搜索网络以将其转换为C#等。基本步骤以及如何使用WMI启动服务。有一个很好的编码和乐趣

sUser = "TESTDomain\T-CL-S"
sPass = "Temp1234"  

Set ServiceSet = GetObject("winmgmts:").ExecQuery("Select * from Win32_Service where Name = 'netlogon'")

For Each Service In ServiceSet
   Service.StopService
   Service.Change "netlogon",Service.PathName, , ,"Automatic",false,sUser,sPass
   Service.StartService
Next

Set Service = Nothing
Set ServiceSet = Nothing

我需要更改什么(我是一名新开发人员…?查看我的更新,它仍然不工作,我是否做错了什么?@user1860934 foreach在编码中意味着什么?SelectQuery正在做什么??检查所有流程