【问题标题】:Execute a process in a remote machine using WMI使用 WMI 在远程机器上执行进程
【发布时间】:2013-10-28 08:24:47
【问题描述】:

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

    }
}

【问题讨论】:

标签: c# process wmi wmi-query


【解决方案1】:

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

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

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

object theProcessToRun = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);

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

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

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

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

【讨论】:

  • 我需要改变什么(我是一个新开发者...)?
  • 查看我的更新,它仍然无法正常工作,我做错了吗?
  • @user1860934 foreach 在编码中意味着什么? SelectQuery 在做什么??
  • 遍历所有进程
【解决方案2】:

这是我在此之前使用 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

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多