Discussion:
WMI Query access denied?
(too old to reply)
zb
2008-01-16 23:10:11 UTC
Permalink
Scenario: This is a C# Windows Form. Works great for my local machine,
but fails with following message:
'System.UnauthorizedAccessException'
Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))

All I have to do is get the MAC address of remote machines. This is a
domain environment and NOT a workgroup. I did enable remote access for
the user on remote machine's WMI Control's namespace.

public class TheWorks
{
private const string CLASS_NETWORKADAPTER =
"Win32_NetworkAdapterConfiguration";
private const string QUERY_NETWORKADAPTER = "SELECT * FROM
Win32_NetworkAdapterConfiguration";
private const string PROPERTY_MACADDRESS = "MacAddress";
private const string PROPERTY_IPADDRESS = "IPAddress";

private ManagementClass _managementClass = null;
private ManagementObjectCollection _managementCollection =
null;
private ManagementScope _managementScope = null;
private ObjectQuery _query = null;
private bool _queryComplete = false;

private string _computerName = null;

public TheWorks()
{
this._managementClass = new ManagementClass(new
ManagementPath(CLASS_NETWORKADAPTER));
this._query = new ObjectQuery(QUERY_NETWORKADAPTER);
}

public TheWorks(string ComputerName): this()
{
this._computerName = ComputerName;
}

public string[] GetMacAddress()
{
System.Collections.ArrayList list = new
System.Collections.ArrayList();

this.runQuery();

foreach (ManagementObject o in this._managementCollection)
{
if (o[PROPERTY_IPADDRESS] != null &
o[PROPERTY_MACADDRESS] != null)
{
list.Add(o[PROPERTY_IPADDRESS].ToString() +
o[PROPERTY_MACADDRESS].ToString());
}
}

return (string[])list.ToArray(typeof(string));
}

public int ResultCount
{
get
{
if (this._managementCollection == null ||
this._queryComplete == false)
{
return -1;
}
else
{
return this._managementCollection.Count;
}
}
}

public string ComputerName
{
get
{
return this._computerName;
}

set
{
this._computerName = value;
}
}

private void runQuery()
{
try
{
if (this._queryComplete == false)
{
this._managementCollection =
this.getClass().GetInstances();
this._queryComplete = true;
}
}
catch (Exception ex)
{
throw ex;
}
}

private ConnectionOptions getConnectionOption()
{
ConnectionOptions conn = new ConnectionOptions();
conn.Timeout = new TimeSpan(0, 0, 0, 10);
conn.Authentication = AuthenticationLevel.Default;
conn.EnablePrivileges = true;
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = @"domain\username";
conn.Password = "my_lovely_password";

return conn;
}

private ManagementScope getScope()
{
if (string.IsNullOrEmpty(this.ComputerName))
{
this._managementScope = new ManagementScope(@"\\" +
Environment.MachineName + @"\root\cimv2");
}
else
{
this._managementScope = new ManagementScope(@"\\" +
this.ComputerName + @"\root\cimv2");
}

this._managementScope.Options =
this.getConnectionOption();
this._managementScope.Connect();
return this._managementScope;
}

private ManagementClass getClass()
{
this._managementClass.Scope = this.getScope();
return this._managementClass;
}
}
chen
2008-02-19 20:03:50 UTC
Permalink
Post by zb
Scenario: This is a C# Windows Form. Works great for my local machine,
    'System.UnauthorizedAccessException'
    Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))
All I have to do is get the MAC address of remote machines. This is a
domain environment and NOT a workgroup. I did enable remote access for
the user on remote machine's WMI Control's namespace.
public class TheWorks
    {
        private const string CLASS_NETWORKADAPTER =
"Win32_NetworkAdapterConfiguration";
        private const string QUERY_NETWORKADAPTER = "SELECT * FROM
Win32_NetworkAdapterConfiguration";
        private const string PROPERTY_MACADDRESS = "MacAddress";
        private const string PROPERTY_IPADDRESS = "IPAddress";
        private ManagementClass _managementClass = null;
        private ManagementObjectCollection _managementCollection =
null;
        private ManagementScope _managementScope = null;
        private ObjectQuery _query = null;
        private bool _queryComplete = false;
        private string _computerName = null;
        public TheWorks()
        {
            this._managementClass = new ManagementClass(new
ManagementPath(CLASS_NETWORKADAPTER));
            this._query = new ObjectQuery(QUERY_NETWORKADAPTER);
        }
        public TheWorks(string ComputerName): this()
        {
            this._computerName = ComputerName;
        }
        public string[] GetMacAddress()
        {
            System.Collections.ArrayList list = new
System.Collections.ArrayList();
            this.runQuery();
            foreach (ManagementObject o in this._managementCollection)
            {
                if (o[PROPERTY_IPADDRESS] != null &
o[PROPERTY_MACADDRESS] != null)
                {
                    list.Add(o[PROPERTY_IPADDRESS].ToString() +
o[PROPERTY_MACADDRESS].ToString());
                }
            }
            return (string[])list.ToArray(typeof(string));
        }
        public int ResultCount
        {
            get
            {
                if (this._managementCollection == null ||
this._queryComplete == false)
                {
                    return -1;
                }
                else
                {
                    return this._managementCollection.Count;
                }
            }
        }
        public string ComputerName
        {
            get
            {
                return this._computerName;
            }
            set
            {
                this._computerName = value;
            }
        }
        private void runQuery()
        {
            try
            {
                if (this._queryComplete == false)
                {
                    this._managementCollection =
this.getClass().GetInstances();
                    this._queryComplete = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private ConnectionOptions getConnectionOption()
        {
            ConnectionOptions conn = new ConnectionOptions();
            conn.Timeout = new TimeSpan(0, 0, 0, 10);
            conn.Authentication = AuthenticationLevel.Default;
            conn.EnablePrivileges = true;
            conn.Impersonation = ImpersonationLevel.Impersonate;
            conn.Password = "my_lovely_password";
            return conn;
        }
        private ManagementScope getScope()
        {
            if (string.IsNullOrEmpty(this.ComputerName))
            {
            }
            else
            {
            }
            this._managementScope.Options =
this.getConnectionOption();
            this._managementScope.Connect();
            return this._managementScope;
        }
        private ManagementClass getClass()
        {
            this._managementClass.Scope = this.getScope();
            return this._managementClass;
        }
}- Hide quoted text -
- Show quoted text -
Take a look at this for enabling WMI for non-admin user accounts:

http://www.poweradmin.com/help/enableWMI.aspx

Loading...