C # Active Directory through WMI

Does anyone have an example of accessing Active Directory, LDAP queries using the WMI namespace (System.Management), and not the System.DirectoryServices namespace.

Here on the MSDN page is described a little using the CIM classes http://msdn.microsoft.com/en-us/library/aa392320(v=VS.85).aspx But I can not find an example of C # that implements it.

For example, to access some Win32 class, you must initialize a Scope object to use the CIMV2 namespace

private ConnectionOptions connection;
private ManagementScope scope;
...
connection = new ConnectionOptions();
...
scope = new ManagementScope("\\\\" + computer + "\\root\\CIMV2", connection);
try
{
   scope.Connect();
}

And use the ObjectQuery class to query for WMI data

ObjectQuery objectQuery = new ObjectQuery("SELECT Name FROM Win32_Processor");
ManagementObjectSearcher searcher = ManagementObjectSearcher(scope, objectQuery);
foreach (ManagementObject queryObj in searcher.Get())
{
return queryObj["Name"].ToString();
}

How can I access AD using the same scope? Thank:)

+3
source share

Source: https://habr.com/ru/post/1750275/


All Articles