“访问被拒绝”WMI异常
我正在使用WMI。我想访问远程系统信息。下面的代码工作环回或本地主机上,但是当我试图访问远程计算机出现下面的异常错误:“访问被拒绝”WMI异常
Access is denied. (Exception from HRESULT:0X8005(E_ACCESSDENIED))
当开关2个系统之间使用。
和
The RPC server Is unavailable. (Exception from HRESULT: 0x800706BA)
当两个系统是直接连接。在两个系统上
OS:Windows服务包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);
}
这可能是很多事情,但一开始,你需要:
- 公开赛在RPC通信防火墙
- 启用远程rpc呼叫
见:http://support.microsoft.com/kb/895085(虽然这涵盖了一个稍微不同的问题的决议是相关的),如果你想查询中使用您所创建的管理范围,你应该使用它的构造函数的另一个重载
tnkz亲爱的, 防火墙被阻止和rpc WMI和远程访问服务正在运行 – ADIL 2009-12-22 15:25:30
。我怀疑,你之间漏掉了代码? 如果您在ConnectionOptions中使用过凭据,那么ManagementObjectServer将不会使用它们。
尝试:
ManagementObjectSearcher srcd;
srcd = new ManagementObjectSearcher
(
_Scope, new ObjectQuery("select * from Win32_DisplayConfiguration")
);
寻找它在MSDN参考。
注:如果不指定凭据,运行用户的凭据将被使用,因此这些都必须有效访问远程计算机,通常,这个账户必须是在远程箱管理员(不对于所有对象,但只是可以肯定)。
如果您使用在两台计算机上都有效的域帐户登录,它可以立即使用。
如果您不在域环境中,只需指定凭据即可。
试试这个:
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 - mabra
我可以查询此通过做srcd.get()但读取任何东西(即使计数属性)失败与ManagementException,告诉我只有'无效的类'没有内部异常... – Lorgarn 2014-11-04 13:32:42
如SO质疑asp classic - Access Denied errors accessing IIS WMI provider from ASP描述这可能是同样的问题。
正如我在回答上述问题时所解释的那样,我在服务器上检查了事件日志(“Windows日志”),我试图通过WMI远程访问IIS,并且我发现用以下文本的事件:
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通过@mabra提出这个问题纳入相关的灵感。以下是我添加的示例C#代码,似乎为我解决了这个问题:
ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
ManagementScope managementScope = new ManagementScope(@"\\remote-server\root\WebAdministration", options);
// ...
@dtb:感谢您的编辑。由于编辑使他们不再相关,我清除了评论并关闭了投票。 – 2009-12-12 21:26:32
关于它的任何解决方案? – Kiquenet 2012-02-10 12:30:42