"访问被拒绝"异常与WMI [英] "Access is denied" Exception with WMI

查看:139
本文介绍了"访问被拒绝"异常与WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


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

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

当在两个系统之间使用开关时。

When switch is used between 2 systems.


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

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

当两个系统都直接连接时。

When both the systems are directly connected.


两个系统上的操作系统:Windows Service Pack 2.

防火墙=已阻止。

远程procedure service = running。

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

工具:.NET Visual Studio 2008 C#

Tool : .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);
}


推荐答案

注意:指定凭证,将使用正在运行的用户的凭据,因此这些必须有效访问远程计算机,通常,此帐户必须是远程框上的管理员(不是所有对象,但只是确定)

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.

尝试:


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.

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

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

br--mabra

这篇关于"访问被拒绝"异常与WMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆