【问题标题】:c# RPC server error when query for active usernamec# 查询活动用户名时出现 RPC 服务器错误
【发布时间】:2017-02-02 12:08:26
【问题描述】:

我有一个使用 WMI 连接到远程计算机的功能。当我以管理员权限启动它时,系统显示 RPC 服务器错误。

我在 Powershell 中有相同类型的脚本,它工作正常,没有 RPC 服务器错误(所以 WMI 在网络上工作,防火墙没有阻止它。我只在互联网上找到了那种答案),并且公司其他用c#编写的软件在这些电脑上都能正常工作(开发人员不再为公司工作,我无法获得源代码)

无论我使用计算机的名称还是它的 IP 地址,都会出现同样的问题。

static public string GetUsername(string computer)
    {

        string username = string.Empty;
        ManagementScope scope = new ManagementScope("\\\\computer\\root\\cimv2");
        //scope.Connect(); If I activate this line, the error occurs on this line
        string queryString = "select LogonId from win32_logonsession where logontype = 2";
        ManagementObjectSearcher query = new ManagementObjectSearcher(scope, new SelectQuery(queryString));
        foreach (ManagementObject mo in query.Get()) //if scope.connect(); is not activated, it's blocking on this line
        {
            username = mo["LogonId"].ToString();

        }



        return username;
    }

我尝试从 Visual Studio 或直接从 .exe(使用管理员)运行我的软件。我还尝试以编程方式模拟工作站管理员,但问题仍然存在。

提前感谢您的热心帮助。

编辑:

我已经尝试过这样的 connectionOptions 解决方案:

options.Username = "YOUR USERNAME";
options.Password = "YOUR PASSWORD";
options.Authority = "ntlmdomain:YOURDOMAIN";

像这样:

options.Username = "YOUR USERNAME";
options.Password = "YOUR PASSWORD";
options.Authority = "Kerberos:YOURDOMAIN";

并添加了模拟选项

   options.Impersonation = ImpersonationLevel.Impersonate;
   options.EnablePrivileges = true;

还是不行。

【问题讨论】:

  • 我一直使用“ConnectionOptions connOptions = new ConnectionOptions(); connOptions.Impersonation = ImpersonationLevel.Impersonate; connOptions.EnablePrivileges = true;”然后将它们用作我的管理范围参数的第二个选项
  • 您能否添加对 sn-p 抛出的错误消息的更详细描述?这可能有助于缩小原因。
  • 感谢您的提议,但不幸的是,它也不起作用。
  • 不幸的是,它是法语的(我不管理服务器......我会用英语安装 Visual Studio!)但这里是: Une exception de type 'System.Runtime.InteropServices. COMException' s'est produite dans System.Management.dll mais n'a pas été gérée dans le code utilisateur Informations supplémentaires : RPC 服务器不可用。 (HRESULT 异常:0x800706BA)
  • 您确定Powershell 脚本没有使用PS-remoting 并在远程机器上本地执行WMI 查询吗?这可以解释为什么 PS 脚本有效,但您的 C# 代码无效。

标签: c# wmi


【解决方案1】:

您是否尝试过像这样为管理范围设置用户名和密码?

static public string GetUsername(string computer)
{
    string username = string.Empty;
    ConnectionOptions options = new ConnectionOptions();
    options.Username = "YOUR USERNAME";
    options.Password = "YOUR PASSWORD";
    options.Authority = "ntlmdomain:YOURDOMAIN";
    ManagementScope scope = new ManagementScope("\\\\computer\\root\\cimv2",options);
    //scope.Connect(); If I activate this line, the error occurs on this line
    string queryString = "select LogonId from win32_logonsession where logontype = 2";
    ManagementObjectSearcher query = new ManagementObjectSearcher(scope, new SelectQuery(queryString));
    foreach (ManagementObject mo in query.Get()) //if scope.connect(); is not activated, it's blocking on this line
    {
        username = mo["LogonId"].ToString();
    }
    return username;
}

有关详细信息,请查看 ManagementScope here 的 MSDN 页面 而对于 ConnectionOptions here。

【讨论】:

  • 我已经尝试过这个解决方案,但它不起作用。我再次尝试以防我做错了什么,但无济于事。我已经阅读了 MSDN 文档,但在我的情况下似乎没有任何效果。
  • ConnectionOptions 有更多属性,正如 BugFinder 在他的评论中提到的那样。我最好的建议是使用这些并再次检查您用于模拟的用户的权限。我建议你在问题中写下你已经尝试过这个解决方案,所以“新”读者知道不要建议这个。
【解决方案2】:

好的,我明白了……我的错误就在这条线

ManagementScope scope = new ManagementScope("\\\\computer\\root\\cimv2",options);

应该是的

ManagementScope scope = new ManagementScope("\\\\" + computer + "\\root\\cimv2",options); 

变量 computer 在字符串中无法识别,因为它在 Powershell 中......我的错!

感谢所有尝试提供帮助的人...下次我会更加小心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多