繁体   English   中英

“访问被拒绝”WMI异常

[英]“Access is denied” Exception with WMI

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

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

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

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);
}

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

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

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

试试这个:

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

这可能是很多事情,但首先你需要:

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

请参阅: http//support.microsoft.com/kb/895085 (虽然这涉及一个稍微不同的问题,但解决方案是相关的)

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

尝试:

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

在MSDN参考中查找它。

这可能与SO问题asp经典中描述的问题相同- 从ASP访问IIS WMI提供程序的访问被拒绝错误

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

无法访问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);
// ...

暂无
暂无

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

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