C# WMI远程进程执行

3

我正在尝试在Win 2008上运行一个调用批处理文件的命令。(当我登录到Win 2008并单击时,该命令可以成功运行)。

但是,当我使用相同的用户凭据通过WMI调用此批处理文件时,批处理文件不会执行。

我的连接代码如下:

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"];

非常感谢您的帮助...


这段代码执行后的 ReturnValue 值是多少? - RRUZ
返回值为0。我认为当没有错误发生时返回此值。 - Jimmy
然后执行了命令,但您无法看到它。因为无法使用 create 方法远程启动交互式进程。 - RRUZ
您正在使用的帐户是否具有“root\cimv2”命名空间的执行方法和远程启用权限? - RRUZ
当我使用DD.exe执行另一个批处理文件并使用不同的命令时,它会在相同的用户帐户下正常执行。 - Jimmy
显示剩余3条评论
2个回答

0
如果在您的服务器上将ROOT\CIMV2设置为脚本的默认命名空间,则您只需要以下内容:
ManagementScope manScope = new ManagementScope(
    String.Format(@"\\{0}", ComputerName), connOptions);
manScope.Connect();

0

在通过WMI远程计算机实例化命令时,您需要指定明确的凭据。WMI增强了安全性,但实际上降低了安全性,因为明确的凭据是以明文形式传递的,而不像令牌那样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接