Converting a policy name to a GUID

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

Feedback

# Links (4/23/2007)

left by Member Blogs at 4/25/2007 9:37 PM Gravatar
.NET Handling Events in Unit Tests with anonymous delegates How to Create a Custom WPF Control WCF Virtual
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: