C# WMI 权限

发布于 2024-11-18 00:49:38 字数 1176 浏览 3 评论 0原文

我搜索了许多关于如何将 VBS 转换为 C# 以及所有这些好东西的问题。

我遇到的问题是,当进程在其运行的远程计算机上执行时,VBS 代码(见下文)使用系统帐户。
当我使用 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;
    }
}

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 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-11-25 00:49:38

您需要设置 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 });

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 });
哭泣的笑容 2024-11-25 00:49:38

这可能是我的白痴错误,但我收到的错误是因为我没有指定它启动计划作业的时间(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);   

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);   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文