2

我想打开进程 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);

    }
}
4

2 回答 2

2

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

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

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

object theProcessToRun = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);

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

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

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

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

于 2013-10-28T08:32:33.737 回答
-1

这是我在此之前使用 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
于 2015-03-07T17:32:05.040 回答