简体   繁体   English

“访问被拒绝”WMI异常

[英]“Access is denied” Exception with WMI

I am working on WMI. 我正在研究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)) (HRESULT异常:0X8005(E_ACCESSDENIED))

When switch is used between 2 systems. 在两个系统之间使用开关时。

and

The RPC server Is unavailable. RPC服务器不可用。 (Exception from HRESULT: 0x800706BA) (HRESULT异常:0x800706BA)

When both the systems are directly connected. 当两个系统直接连接时。


OS on both systems: Windows Service Pack 2. 两个系统上的操作系统:Windows Service Pack 2。
Firewalls = blocked. 防火墙=被阻止。
Remote procedure service = running. 远程过程服务=正在运行。

Tool : .NET Visual Studio 2008 C# 工具:.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);
}

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. 从我的角度来看,问题出现了,因为您没有在ManagementPath中指定远程计算机。 A ManagementPath, created with the defaults, always points to the local machine. 使用默认值创建的ManagementPath始终指向本地计算机。 And if you just specify credentials to the local computer, this is not allowed and always fails. 如果您只是为本地计算机指定凭据,则不允许这样做并始终失败。

br--mabra BR - mabra

It could be many things, but for a start you need to: 这可能是很多事情,但首先你需要:

  • Open for RPC traffic in the firewall 在防火墙中打开RPC流量
  • Enable remote rpc calls 启用远程rpc调用

See: http://support.microsoft.com/kb/895085 (Although this covers a slightly different problem the resolution is relevant) 请参阅: http//support.microsoft.com/kb/895085 (虽然这涉及一个稍微不同的问题,但解决方案是相关的)

If you want your query to use your created ManagementScope, you should use another overload of it's constructor. 如果您希望查询使用您创建的ManagementScope,则应使用其构造函数的另一个重载。 I suspect, you have omitted code between? 我怀疑,你之间省略了代码? If you've had used credentials in your ConnectionOptions, your ManagementObjectServer will not use them. 如果您已在ConnectionOptions中使用过凭据,则ManagementObjectServer将不会使用它们。

Try: 尝试:

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

Look for it in the MSDN reference. 在MSDN参考中查找它。

This may be the same issue as described in the SO question asp classic - Access Denied errors accessing IIS WMI provider from ASP . 这可能与SO问题asp经典中描述的问题相同- 从ASP访问IIS WMI提供程序的访问被拒绝错误

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: 正如我在回答上述问题时所解释的,我检查了我试图通过WMI远程访问IIS的服务器上的事件日志(“Windows Logs”),并且看到我发现了一个事件以下文字:

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. 无法访问root \\ WebAdministration命名空间,因为命名空间标记为RequiresEncryption,但脚本或应用程序尝试使用低于Pkt_Privacy的身份验证级别连接到此命名空间。 Change the authentication level to Pkt_Privacy and run the script or application again. 将身份验证级别更改为Pkt_Privacy并再次运行脚本或应用程序。

The answer offered by @mabra to this question included the relevant inspiration. @mabra为这个问题提供的答案包括相关的灵感。 Here's the example C# code that I added that seemed to resolve this issue for me: 这是我添加的示例C#代码,似乎为我解决了这个问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM