从另一台远程计算机上的共享启动远程计算机上的WMI进程

时间:2012-01-27 12:41:30

标签: c# process wmi

我有以下代码在远程计算机上从第二台远程计算机上的共享运行进程,如图所示:

Connection http://i.msdn.microsoft.com/dynimg/IC116011.png

public class Runner
{
    public static string RunExecutable(string machine, string executable, string username, string password, string domain)
    {
        try
        {
            ConnectionOptions connectionOptions = new ConnectionOptions();
            connectionOptions.Authority = "kerberos:" + domain + @"\" + machine;
            connectionOptions.Username = username;
            connectionOptions.Password = password;
            connectionOptions.Impersonation = ImpersonationLevel.Delegate;
            connectionOptions.Authentication = AuthenticationLevel.PacketPrivacy;

            //define the WMI root name space
            ManagementScope scope = new ManagementScope(@"\\" + machine + "." + domain + @"\root\CIMV2", connectionOptions);

            //define path for the WMI class
            ManagementPath p = new ManagementPath("Win32_Process");

            //define new instance
            ManagementClass classInstance = new ManagementClass(scope, p, null);

            ManagementClass startupSettings = new ManagementClass("Win32_ProcessStartup");
            startupSettings.Scope = scope;
            startupSettings["CreateFlags"] = 16777216;

            // Obtain in-parameters for the method
            ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");

            // Add the input parameters.
            inParams["CommandLine"] = executable;
            inParams["ProcessStartupInformation"] = startupSettings;

            // Execute the method and obtain the return values.
            ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null);

            // List outParams
            string retVal = outParams["ReturnValue"].ToString();
            return "ReturnValue: " + retVal;
        }

        catch (ManagementException me)
        {
            return me.Message;
        }

        catch (COMException ioe)
        {
            return ioe.Message;
        }
    }
}

我的环境中有5台机器,都在同一个域中。 3个运行Windows Server 2008R2,一个Windows 7和一个Windows XP:

  • 的WinXP
  • Win7的
  • Master2008
  • Slave2008-1
  • Slave2008-2

我从域控制器Master2008运行代码,并尝试在其他计算机上启动进程,但在XP和7计算机上启动进程时遇到一些问题。

在WinXP和Win7机器上启动进程时,我得到的返回值为8,即“未知错误”,但在Server 2008R2计算机上启动该进程时,它可以正常工作。

所有计算机都已在AD中标记为受信任。

我正在尝试启动的进程是\\“machine”\ c $ \ Windows \ System32 \ Calc.exe

我尝试从不同的机器运行该过程,结果如下(该程序正在Master2008上运行):

On WinXP
 - From Win7: Failed (8)
 - From Slave2008-1: Failed (8)
 - From Slave2008-2: Failed (8)
 - From Master2008: Failed (8)

On Win7
 - From WinXP: Success (0)
 - From Slave2008-1: Failed (8)
 - From Slave2008-2: Failed (8)
 - From Master2008: Failed (8)

On Slave2008-1
 - From WinXP: Success (0)
 - From Win7: Success (0)
 - From Slave2008-2: Success (0)
 - From Master2008: Success (0)

On Slave2008-2
 - From WinXP: Success (0)
 - From Win7: Success (0)
 - From Slave2008-1: Success (0)
 - From Master2008: Success (0)

出于某种原因,它们都无法用于WinXP机器,但Win7机器可以从WinXP机器安装。

有没有人知道什么是错的?

2 个答案:

答案 0 :(得分:1)

代码似乎没有问题。我尝试创建一个简单的应用程序而不是“calc.exe”,它可以正常工作。

问题是我试图从32位客户端上的64位服务器启动“calc.exe”。此外,Windows7上的“calc.exe”不会在WindowsXP上运行。

答案 1 :(得分:0)

不要工作。 http://technet.microsoft.com/en-us/library/ee156574.aspx

除非事务中涉及的所有用户帐户和计算机帐户都已在Active Directory中标记为“受信任以进行委派”,否则无法使用“委派”模拟级别。这有助于将安全风险降至最低。虽然远程计算机可以使用您的凭据,但只有当它和该事务中涉及的任何其他计算机都受信任以进行委派时,它才能这样做。