Ok, part of all this we've already talked about requires that you have the policy's GUID. But how do you get it?
Simple enough:
private string Query(string Criteria, string Attribute)
{
DirectorySearcher mySearcher = new DirectorySearcher(new DirectoryEntry("LDAP://DC=DOMAIN,DC=MYCOMPANY,DC=com", null, null, AuthenticationTypes.Secure ));
mySearcher.ClientTimeout = new TimeSpan(0, 0, 10);
mySearcher.Filter = Criteria;
mySearcher.SearchScope = SearchScope.Subtree;
try
{
SearchResult result = mySearcher.FindOne();
//if exception was not thrown, means it connected successfuly
if (result != null)
{
if(result.Properties[Attribute][0] != null)
{
mySearcher.Dispose();
return result.Properties[Attribute][0].ToString();
}
}
}
catch (Exception ex)
{
return ex.Message;
}
mySearcher.Dispose();
return "Not found.";
}
with the provision that the "Criteria" and "Attribute" arguments are, respectively,
"(&(objectCategory=groupPolicyContainer)(name=" + GUID + "))" and "displayName" to convert from GUID to policy name, and
"(&(objectCategory=groupPolicyContainer)(displayName=" + Policy Name + "))","name" to go from policy name to GUID.
Print | posted on Monday, April 23, 2007 6:04 AM