“访问被拒绝” WMI 异常

发布于 2024-08-15 10:38:02 字数 1087 浏览 2 评论 0原文

我正在研究 WMI。我想访问远程系统信息。以下代码适用于环回或本地主机,但当我尝试访问远程计算机时,它显示以下异常错误:

访问被拒绝。 (HRESULT 异常:0X8005(E_ACCESSDENIED))

在 2 个系统之间使用切换时。

RPC 服务器不可用。 (HRESULT 异常:0x800706BA)

当两个系统直接连接时。


两个系统上的操作系统:Windows Service Pack 2。
防火墙 = 被阻止。
远程过程服务=正在运行。

工具:.NET Visual Studio 2008 C#

代码:

try
{
    ConnectionOptions _Options = new ConnectionOptions();
    ManagementPath _Path = new ManagementPath(s);

    ManagementScope _Scope = new ManagementScope(_Path, _Options);
    _Scope.Connect();
    ManagementObjectSearcher srcd = new ManagementObjectSearcher("select * from Win32_DisplayConfiguration");
    foreach (ManagementObject obj in srcd.Get())
    {
        //listBox5.Items.Add(obj.Properties.ToString());
        foreach (PropertyData aProperty in obj.Properties)
        {
            listBox1.Items.Add(aProperty.Name.ToString() + " : " + aProperty.Value);
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

I am working on WMI. I want to access remote system information. The following code is working for loopback or on local host but when I try to access the remote machine it shows the following exception error:

Access is denied. (Exception from HRESULT:0X8005(E_ACCESSDENIED))

When switch is used between 2 systems.

and

The RPC server Is unavailable. (Exception from HRESULT: 0x800706BA)

When both the systems are directly connected.


OS on both systems: Windows Service Pack 2.
Firewalls = blocked.
Remote procedure service = running.

Tool : .NET Visual Studio 2008 C#

Code:

try
{
    ConnectionOptions _Options = new ConnectionOptions();
    ManagementPath _Path = new ManagementPath(s);

    ManagementScope _Scope = new ManagementScope(_Path, _Options);
    _Scope.Connect();
    ManagementObjectSearcher srcd = new ManagementObjectSearcher("select * from Win32_DisplayConfiguration");
    foreach (ManagementObject obj in srcd.Get())
    {
        //listBox5.Items.Add(obj.Properties.ToString());
        foreach (PropertyData aProperty in obj.Properties)
        {
            listBox1.Items.Add(aProperty.Name.ToString() + " : " + aProperty.Value);
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

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

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

发布评论

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

评论(4

最好是你 2024-08-22 10:38:02

注意:如果您不指定凭据,则将使用正在运行的用户的凭据,因此这些凭据必须有效才能访问远程计算机,并且通常该帐户必须是远程机器上的管理员(并非对于所有对象,但只是为了确定)。

如果您使用在两台计算机上都有效的域帐户登录,则它可以开箱即用。

如果您不在域环境中,只需指定凭据即可。

试试这个:

ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.Packet;
co.Timeout = new TimeSpan(0, 0, 30);
co.EnablePrivileges = true;
co.Username = "\\";
co.Password = "";

ManagementPath mp = new ManagementPath();
mp.NamespacePath = @"\root\cimv2";
mp.Server = "";               ///Regard this!!!!

ManagementScope ms = new ManagementScope(mp, co);
ms.Connect();

ManagementObjectSearcher srcd;
srcd = new ManagementObjectSearcher  
(
    ms, new ObjectQuery("select * from Win32_DisplayConfiguration")
);

这对我来说一直有效。

从我的角度来看,出现问题是因为您没有在 ManagementPath 中指定远程计算机。使用默认值创建的 ManagementPath 始终指向本地计算机。如果您只是指定本地计算机的凭据,则这是不允许的并且总是会失败。

br--马布拉

Note:If you do not specify credentials, the credentials of the running user will be used and so these have to be valid to access the remote computer and usually, this account has to be an admin on the remote box (not for all objects, but just to be sure).

If you are logged in with a domain account, which is valid on both computers, it would work out-of-the-box.

If you are not in a domain environment, just specify credentials.

Try this:

ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.Packet;
co.Timeout = new TimeSpan(0, 0, 30);
co.EnablePrivileges = true;
co.Username = "\\";
co.Password = "";

ManagementPath mp = new ManagementPath();
mp.NamespacePath = @"\root\cimv2";
mp.Server = "";               ///Regard this!!!!

ManagementScope ms = new ManagementScope(mp, co);
ms.Connect();

ManagementObjectSearcher srcd;
srcd = new ManagementObjectSearcher  
(
    ms, new ObjectQuery("select * from Win32_DisplayConfiguration")
);

This is working all the time for me.

From my point of view, the problem occurs, because you are not specifying a remote computer in your ManagementPath. A ManagementPath, created with the defaults, always points to the local machine. And if you just specify credentials to the local computer, this is not allowed and always fails.

br--mabra

兰花执着 2024-08-22 10:38:02

这可能与 SO 问题 asp classic - 从 ASP 访问 IIS WMI 提供程序时出现“访问被拒绝”错误

正如我在回答上述问题时所解释的那样,我检查了我尝试通过 WMI 远程访问 IIS 的服务器上的事件日志(“Windows 日志”),你瞧,我发现了一个带有以下内容的事件:以下文字:

对 root\WebAdministration 命名空间的访问被拒绝,因为该命名空间标记有 RequiresEncryption,但脚本或应用程序尝试使用低于 Pkt_Privacy 的身份验证级别连接到此命名空间。将身份验证级别更改为 Pkt_Privacy 并再次运行脚本或应用程序。

@mabra 对此问题提供的答案包含了相关的启发。下面是我添加的示例 C# 代码,它似乎可以为我解决这个问题:

ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementScope managementScope = new ManagementScope(@"\\remote-server\root\WebAdministration", options);
// ...

This may be the same issue as described in the SO question asp classic - Access Denied errors accessing IIS WMI provider from ASP.

As I explained in my answer to the above-mentioned question, I checked the event logs ("Windows Logs") on the server to which I'm attempting to access IIS remotely via WMI, and lo and behold I found an event with the following text:

Access to the root\WebAdministration namespace was denied because the namespace is marked with RequiresEncryption but the script or application attempted to connect to this namespace with an authentication level below Pkt_Privacy. Change the authentication level to Pkt_Privacy and run the script or application again.

The answer offered by @mabra to this question included the relevant inspiration. Here's the example C# code that I added that seemed to resolve this issue for me:

ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementScope managementScope = new ManagementScope(@"\\remote-server\root\WebAdministration", options);
// ...
奈何桥上唱咆哮 2024-08-22 10:38:02

可能有很多事情,但首先您需要:

  • 在防火墙中打开 RPC 流量
  • 启用远程 rpc 调用

请参阅:http://support.microsoft.com/kb/895085(尽管这涵盖了略有不同的问题,但解决方案是相关的)

It could be many things, but for a start you need to:

  • Open for RPC traffic in the firewall
  • Enable remote rpc calls

See: http://support.microsoft.com/kb/895085 (Although this covers a slightly different problem the resolution is relevant)

难如初 2024-08-22 10:38:02

如果您希望查询使用您创建的 ManagementScope,则应该使用其构造函数的另一个重载。我怀疑,你省略了之间的代码吗?
如果您在 ConnectionOptions 中使用了凭据,您的 ManagementObjectServer 将不会使用它们。

尝试:

ManagementObjectSearcher srcd;
srcd = new ManagementObjectSearcher  
(
    _Scope, new ObjectQuery("select * from Win32_DisplayConfiguration")
);

在 MSDN 参考中查找它。

If you want your query to use your created ManagementScope, you should use another overload of it's constructor. I suspect, you have omitted code between?
If you've had used credentials in your ConnectionOptions, your ManagementObjectServer will not use them.

Try:

ManagementObjectSearcher srcd;
srcd = new ManagementObjectSearcher  
(
    _Scope, new ObjectQuery("select * from Win32_DisplayConfiguration")
);

Look for it in the MSDN reference.

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