2011-06-30 68 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