开发者

C# WMI privileges

I have searched through numerous questions about how to convert VBS to C# and all that good stuff.

The issue that I'm having is that with the VBS code (see below) when the process is executed on the remote machine it's run with the SYSTEM account.

When I execute with C# it's run with my credentials (or whomever runs the C# program).

The VBS seems to be more reliable in getting remote installs to go through which is what i need it for.

I wanted to switch to C# so I could make a more user friendly GUI for the program.

Anyone know how to get C# to run WMI with the SYSTEM account?

VBS Code:

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# code:

static public int RemoteAdmin(string remoteMachine, string runFile)
{
    try
    {
        ManagementPath run = new Manag开发者_如何学GoementPath(@"\\" + 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;
    }
}


You need to setup the 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 });


This is probably idiot error on my part but the error I was getting was because I didn't specify a time (UTC format) for it to start the scheduled job

    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);   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜