“访问被拒绝” WMI 异常
我正在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意:如果您不指定凭据,则将使用正在运行的用户的凭据,因此这些凭据必须有效才能访问远程计算机,并且通常该帐户必须是远程机器上的管理员(并非对于所有对象,但只是为了确定)。
如果您使用在两台计算机上都有效的域帐户登录,则它可以开箱即用。
如果您不在域环境中,只需指定凭据即可。
试试这个:
这对我来说一直有效。
从我的角度来看,出现问题是因为您没有在 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:
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
这可能与 SO 问题 asp classic - 从 ASP 访问 IIS WMI 提供程序时出现“访问被拒绝”错误。
正如我在回答上述问题时所解释的那样,我检查了我尝试通过 WMI 远程访问 IIS 的服务器上的事件日志(“Windows 日志”),你瞧,我发现了一个带有以下内容的事件:以下文字:
@mabra 对此问题提供的答案包含了相关的启发。下面是我添加的示例 C# 代码,它似乎可以为我解决这个问题:
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:
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:
可能有很多事情,但首先您需要:
请参阅:http://support.microsoft.com/kb/895085(尽管这涵盖了略有不同的问题,但解决方案是相关的)
It could be many things, but for a start you need to:
See: http://support.microsoft.com/kb/895085 (Although this covers a slightly different problem the resolution is relevant)
如果您希望查询使用您创建的 ManagementScope,则应该使用其构造函数的另一个重载。我怀疑,你省略了之间的代码吗?
如果您在 ConnectionOptions 中使用了凭据,您的 ManagementObjectServer 将不会使用它们。
尝试:
在 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:
Look for it in the MSDN reference.