简体   繁体   中英

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. This is meant to be a WCF operation that user will run from web application.

I implemented the code from codeproject , and my operation fails on InvokeMethod function. The error is 'Access is denied. (Exception from 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. 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? I read that setting wmiProviderEnabled to 'true' in the diagnostics section of web.config might help, but it didn't.

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'. It was not enough, and in my investigation I found a possible bug in the aforementioned codeproject source code. In the StartProcess function located in the ProcessMethod class, the connectionScope was not being added to the ManagementClass. Hence, I added a ManagementScope connectionScope parameter to the constructor and then filled the processTask.Scope. Please take a look at my updated StartPtocess function:

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.

This fixed my problem. Hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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