2012-10-11 93 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();