简体   繁体   中英

C# WMI remote process execution

I am trying to run a command which invokes a batch file on Win 2008 box. (The commands runs successfully when I log in to Win 2008 and click).

But when I call this batch file via WMI using same user credentials the batch does not execute.

My code to connect is:

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
connOptions.Username = UserName;
connOptions.Password = Password;

ManagementScope manScope = new ManagementScope(
    String.Format(@"\\{0}\ROOT\CIMV2", ComputerName), connOptions);
manScope.Connect();

ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(
    manScope, managementPath, objectGetOptions);

ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

inParams["CommandLine"] = command;
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
Object returnValue = outParams["ReturnValue"];

Any help is appreciated...

You need to specify explicit credentials when instantiating a command on a remote computer over WMI. WMI enhances security, but in so doing actually reduces security since explicit credential passes them in the clear, unlike a token.

If ROOT\\CIMV2 is set as the default namespace for scripting on your server then you should just need the following:

ManagementScope manScope = new ManagementScope(
    String.Format(@"\\{0}", ComputerName), connOptions);
manScope.Connect();

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