2011-06-30 69 views
3

我已經搜索了很多關於如何將VBS轉換爲C#和所有好東西的問題。
C#WMI權限

我遇到的問題是使用VBS代碼(見下文)在遠程計算機上執行進程時使用SYSTEM帳戶運行該進程。當我用C#執行它時,它會運行我的憑據(或任何運行C#程序的人)。
VBS在獲得遠程安裝時似乎更可靠,這是我需要的。

我想切換到C#,所以我可以爲程序製作更友好的GUI。
任何人都知道如何讓C#使用SYSTEM帳戶運行WMI?

VBS代碼:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set objScheduledJob = objWMIService.Get("Win32_ScheduledJob") 
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime") 

'add the scheduled job to be run 
objSWbemDateTime.SetVarDate(DateAdd(INTERVAL, MINUTES, Now())) 
errReturn = objScheduledJob.Create(strCommand, objSWbemDateTime.Value, False, 0, 0, True, intJobID) 

C#代碼:

static public int RemoteAdmin(string remoteMachine, string runFile) 
{ 
    try 
    { 
     ManagementPath run = new ManagementPath(@"\\" + remoteMachine + @"\root\cimv2:Win32_process"); 
     ManagementClass man = new ManagementClass(run); 

     man.InvokeMethod("Create", new Object[] { runFile }); 
     return 0; 
    } 
    catch 
    { 
     MessageBox.Show("Error in remote execution"); 
     return 1; 
    } 
} 
+0

你看看ConnectionOptions類嗎? – Roly

回答

0

這是我的一部分可能是白癡的錯誤,但我是錯誤因爲我沒有指定時間(UTC格式)來啓動預定作業

ConnectionOptions connOptions = new ConnectionOptions(); 
    connOptions.Impersonation = ImpersonationLevel.Impersonate; 
    connOptions.Authentication = AuthenticationLevel.PacketPrivacy; 
    connOptions.EnablePrivileges = true; 
    ManagementScope manScope = new ManagementScope(String.Format(@"\\" + remoteMachine + @"\ROOT\CIMV2"), connOptions); 
    manScope.Connect(); 
    ObjectGetOptions objectGetOptions = new ObjectGetOptions(); 
    ManagementPath managementPath = new ManagementPath("Win32_ScheduledJob"); 
    ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions); 
    ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); 
    inParams["Command"] = runFile; 
    string StartTime = DateTimetoUTC(DateTime.Now.AddMinutes(1)); 
    inParams["StartTime"] = StartTime; 
    ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null); 
2

您需要設置ConnectionOptions

ConnectionOptions wmiConnOpts = new ConnectionOptions(); 
wmiConnOpts.Impersonation = ImpersonationLevel.Impersonate; 
wmiConnOpts.Authentication = System.Management.AuthenticationLevel.Default; 
wmiConnOpts.EnablePrivileges = true; 

ManagementScope wmiLoc = 
    new ManagementScope(String.Format(@"\\{0}\root\cimv2", remoteMachine), 
     wmiConnOpts); 
ManagementPath wmiProcPath = new ManagementPath("Win32_ScheduledJob"); 
ManagementClass wmiProc = new ManagementClass(wmiLoc, wmiProcPath, null); 
wmiProc.InvokeMethod("Create", new Object[] { runFile }); 
+0

非常感謝您的快速回復。我必須承認,我不是100%的C#(這可能很明顯)。我收到一行錯誤: – DarkShadow

+0

抱歉...「wmiProc.InvokeMethod(」Create「,new Object [] {runFile});錯誤指出有一個無效參數runFile at the time =」cmd .exe/c taskkill/f/im iexplore.exe「 – DarkShadow

+0

任何想法爲什麼上面的代碼在運行並傳遞主機名和」cmd.exe/c taskkill/f/im iexplore.exe「時會出現無效的參數錯誤。 。還嘗試傳遞其他幾個命令,如msi安裝,常規exe安裝和註冊表工作。在調試過程中所有失敗的消息都是相同的。 – DarkShadow