You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#以非管理员身份远程执行WMI查询遇权限拒绝问题求助

非管理员权限下C#远程WMI查询实现方案

关键差异说明

PowerShell的Get-WmiObject默认使用WMI DCOM协议,而C#中若使用CimSession,即便配置DComSessionOptions,也需严格匹配协议的权限要求和细节配置——这是导致权限报错的核心原因。

可行C#实现代码

方式1:使用当前运行应用的非管理员用户

using System;
using System.Management;

class Program
{
    static void Main(string[] args)
    {
        string remotePcName = "pcname";
        string wmiQuery = "SELECT * FROM Win32_ComputerSystem";

        ConnectionOptions options = new ConnectionOptions
        {
            Impersonation = ImpersonationLevel.Impersonate,
            Authentication = AuthenticationLevel.Default,
            EnablePrivileges = false // 非管理员用户无需启用特权
        };

        ManagementScope scope = new ManagementScope($"\\\\{remotePcName}\\root\\cimv2", options);

        try
        {
            scope.Connect();
            ObjectQuery query = new ObjectQuery(wmiQuery);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in searcher.Get())
            {
                Console.WriteLine($"计算机名: {obj["Name"]}");
                Console.WriteLine($"制造商: {obj["Manufacturer"]}");
                Console.WriteLine($"型号: {obj["Model"]}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }
}

方式2:显式指定非管理员凭据

using System;
using System.Management;

class Program
{
    static void Main(string[] args)
    {
        string remotePcName = "pcname";
        string wmiQuery = "SELECT * FROM Win32_ComputerSystem";
        string username = "domain\\nonadminuser";
        string password = "userpassword";

        ConnectionOptions options = new ConnectionOptions
        {
            Impersonation = ImpersonationLevel.Impersonate,
            Authentication = AuthenticationLevel.Default,
            Username = username,
            Password = password,
            EnablePrivileges = false
        };

        ManagementScope scope = new ManagementScope($"\\\\{remotePcName}\\root\\cimv2", options);

        try
        {
            scope.Connect();
            ObjectQuery query = new ObjectQuery(wmiQuery);
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject obj in searcher.Get())
            {
                Console.WriteLine($"计算机名: {obj["Name"]}");
                Console.WriteLine($"制造商: {obj["Manufacturer"]}");
                Console.WriteLine($"型号: {obj["Model"]}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message}");
        }
    }
}

权限配置补全项

若仍报错,检查以下配置是否到位:

  • 在远程主机的wmimgmt.msc中,对root\cimv2命名空间,给目标用户授予**远程启用(Remote Enable)**权限(需明确勾选该选项,而非仅授予“全部权限”)
  • 确保目标用户已加入远程主机的Distributed COM Users组,同时在组件服务(dcomcnfg)中完成以下操作:
    1. 展开「组件服务」→「计算机」→「我的电脑」,右键选择「属性」→「COM安全」选项卡
    2. 在「启动和激活权限」中点击「编辑限制」,给Distributed COM Users组勾选「远程启动」和「远程激活」权限
    3. 在「访问权限」中点击「编辑限制」,给Distributed COM Users组勾选「远程访问」权限
  • 确认远程主机的WMI服务(Winmgmt)已正常运行,且未限制非管理员访问

为什么CimSession会报错?

Microsoft.Management.Infrastructure(CimSession)默认优先使用WinRM协议,即便配置DComSessionOptions,也可能存在协议协商问题。而System.Management直接调用传统WMI DCOM接口,和PowerShell的Get-WmiObject底层逻辑一致,因此更易匹配权限配置。

内容的提问来源于stack exchange,提问作者senioradmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:13:14