2012-10-11 92 views
3

我想运行一个命令,在Win 2008框中调用批处理文件。 (当我登录到Win 2008并单击时,命令运行成功)。C#WMI远程进程执行

但是,当我通过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"]; 

任何帮助表示赞赏...

+0

这是该代码的执行后的返回值的价值? – RRUZ

+0

返回值为0 ..我认为在没有错误时会返回。 – Jimmy

+0

然后执行该命令,但对您不可见。因为创建方法不能用于远程启动交互式进程。 – RRUZ

回答

0

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

0

如果ROOT \ CIMV2被设置为脚本的默认命名空间的服务器上,那么你应该只需要以下条件:

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