当对类Win32_DiskDrive使用非管理员用户时,WMI给出了通用故障

时间:2019-06-14 04:01:32

标签: wmi

我为非管理员帐户提供了appropriate permission,并且该用户已添加到“分布式COM用户”组中。

尝试访问Win32_DiskDriveWin32_DiskDriveToDiskPartition类时,出现Generic Failure错误。是否需要任何额外的许可?

  • 如果我使用的是管理员帐户,那同样有效。
  • 其他类,例如Win32_NetworkAdapterConfiguration使用非管理员帐户给出结果

       if (!String.IsNullOrWhiteSpace(username) && !String.IsNullOrWhiteSpace(password))
                {
                    ConnectionOptions connectionOptions = new ConnectionOptions
                    {
                        Impersonation = ImpersonationLevel.Impersonate,
                        Authentication = AuthenticationLevel.PacketPrivacy,
                        Timeout = TimeSpan.FromSeconds(60),
                        Username = username,
                        Password = password
                    };
    
                    var managementScope = new ManagementScope(@"\\" + assetNameOrIpAddress + @"\root\cimv2", connectionOptions);
                    managementScope.Connect();
                    managementObjectSearcher.Scope = managementScope;
                }
    

1 个答案:

答案 0 :(得分:0)

您应该添加正确的权限

       ConnectionOptions connOptions = new ConnectionOptions();
       connOptions.Impersonation = ImpersonationLevel.Impersonate;
       //Add AuthenticationLevel that suits your need
       connOptions.Authentication = AuthenticationLevel.PacketPrivacy; 
       connOptions.EnablePrivileges = true;

       ManagementScope scope =
  new ManagementScope("MANAGEMNET_PATH"
, connOptions);
       scope.Connect();


       ObjectQuery query = new ObjectQuery(
           "YOUR QUERY");
       ManagementObjectSearcher searcher =
           new ManagementObjectSearcher(scope, query);