简体   繁体   English

使用WCF通过WMI启动远程进程

[英]Using WCF to start a remote process by WMI

I wanted to start a process (not interactive, small console application) on a remote machine by making a call to WMI from WCF service. 我想通过从WCF服务调用WMI在远程计算机上启动一个进程(非交互式,小型控制台应用程序)。 This is meant to be a WCF operation that user will run from web application. 这是用户将从Web应用程序运行的WCF操作。

I implemented the code from codeproject , and my operation fails on InvokeMethod function. 我实现了codeproject中的代码,并且对InvokeMethod函数的操作失败。 The error is 'Access is denied. 错误是“访问被拒绝。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)'. (来自HRESULT的异常:0x80070005(E_ACCESSDENIED)”。

I tested the WMI connection by using WBEMTEST tool and it works great with the same set of parameters (path to remote server, user name/password, path to application meant to run) both from PC in the same and in the other domain. 我使用WBEMTEST工具测试了WMI连接,它在同一域和另一域中的PC上都可以与同一组参数(到远程服务器的路径,用户名/密码,要运行的应用程序的路径)一起使用,效果很好。 The account used for impersonation was set up like in this tutorial . 本教程中一样设置用于模拟的帐户。 Since it works with WBEMTEST, are there any specific things to check when using WCF to make this call? 由于它与WBEMTEST一起使用,因此在使用WCF进行此调用时是否需要检查某些特定的事项? I read that setting wmiProviderEnabled to 'true' in the diagnostics section of web.config might help, but it didn't. 我读到在web.config的诊断部分将wmiProviderEnabled设置为'true'可能会有所帮助,但没有帮助。

Finally I was able to fix this issue. 最后,我能够解决此问题。

First, I applied to my configuration everything what is described in this article under paragraphs 'Enabling WMI' and 'Security'. 首先,我将本文在“启用WMI”和“安全性”段落中描述的所有内容应用于配置。 It was not enough, and in my investigation I found a possible bug in the aforementioned codeproject source code. 这还不够,在我的调查中,我发现上述codeproject源代码中可能存在错误。 In the StartProcess function located in the ProcessMethod class, the connectionScope was not being added to the ManagementClass. 在ProcessMethod类中的StartProcess函数中,未将connectionScope添加到ManagementClass中。 Hence, I added a ManagementScope connectionScope parameter to the constructor and then filled the processTask.Scope. 因此,我向构造函数添加了ManagementScope connectionScope参数,然后填充了processTask.Scope。 Please take a look at my updated StartPtocess function: 请看看我更新的StartPtocess函数:

public static int StartProcess(string machineName, string processPath, ManagementScope connnectionScope, int timeout)
{
    ManagementClass processTask = new ManagementClass(@"\\" + machineName + @"\root\CIMV2",
                                                                    "Win32_Process", null);
    processTask.Scope = connnectionScope;
    ManagementBaseObject methodParams = processTask.GetMethodParameters("Create");
    methodParams["CommandLine"] = processPath;
    InvokeMethodOptions options = new InvokeMethodOptions();
    options.Timeout = TimeSpan.FromSeconds(timeout);
    ManagementBaseObject exitCode = processTask.InvokeMethod("Create", methodParams, null);

    return Convert.ToInt32(exitCode["ReturnValue"].ToString());
}

ManagementScope is created in ProcessLocal/ProcessRemote constructors. ManagementScope在ProcessLocal / ProcessRemote构造函数中创建。

This fixed my problem. 这解决了我的问题。 Hope it helps someone. 希望它可以帮助某人。

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

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