简体   繁体   English

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

[英]Start process with WMI on remote machine from a share on another remote machine

I have the following code to run a process on a remote machine from a share on a second remote machine as described in the image: 我有以下代码在远程计算机上从第二台远程计算机上的共享运行进程,如图所示:

连接
(source: microsoft.com ) (来源: microsoft.com

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;
        }
    }
}

I have 5 machines in my environment, all in the same domain. 我的环境中有5台机器,都在同一个域中。 3 are running Windows Server 2008R2, one Windows 7 and one Windows XP: 3个运行Windows Server 2008R2,一个Windows 7和一个Windows XP:

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

I run the code from Master2008, the domain controller, and try to start a process on the other machines, but run into some problems when starting a process on the XP and 7 machines. 我从域控制器Master2008运行代码,并尝试在其他计算机上启动进程,但在XP和7计算机上启动进程时遇到一些问题。

When starting the process on the WinXP and Win7 machines i get a return value of 8, which is "Unknown error", but when starting the process on the Server 2008R2 machines it works without problems. 在WinXP和Win7机器上启动进程时,我得到的返回值为8,即“未知错误”,但在Server 2008R2计算机上启动该进程时,它可以正常工作。

All the machines has been marked as trusted for delegation in AD. 所有机器都已被标记为AD中的委托信任。

The process I'm trying to start is \\\\"machine"\\c$\\Windows\\System32\\Calc.exe 我正在尝试启动的进程是\\\\“machine”\\ c $ \\ Windows \\ System32 \\ Calc.exe

I've tried running the process from different machines, and the result was the following (The program is beeing run on Master2008): 我尝试从不同的机器运行该过程,结果如下(该程序正在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)

For some reason, they all fail for WinXP machine, but the Win7 machine can install from the WinXP machine. 出于某种原因,它们都无法用于WinXP机器,但Win7机器可以从WinXP机器安装。

Does anyone have any idea what can be wrong? 有谁知道什么是错的?

It seems there were no problem with the code. 似乎代码没有问题。 I tried to make a simple application to start instead of "calc.exe" and it worked as it should. 我试图创建一个简单的应用程序而不是“calc.exe”,它应该工作。

The problem was that I was trying to start "calc.exe" from 64bit servers on a 32bit clients. 问题是我试图从32位客户端上的64位服务器启动“calc.exe”。 Also, "calc.exe" on Windows7 wont run on WindowsXP. 此外,Windows7上的“calc.exe”不会在WindowsXP上运行。

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

You cannot use the Delegate impersonation level unless all the user accounts and computer accounts involved in the transaction have all been marked as Trusted for delegation in Active Directory. 除非事务中涉及的所有用户帐户和计算机帐户都已在Active Directory中标记为受信任以进行委派,否则无法使用委派模拟级别。 This helps minimize the security risks. 这有助于将安全风险降至最低。 Although a remote computer can use your credentials, it can do so only if both it and any other computers involved in the transaction are trusted for delegation. 虽然远程计算机可以使用您的凭据,但只有当它和该事务中涉及的任何其他计算机都受信任以进行委派时,它才能使用您的凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM