Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need To Determine Distinguished Name For An OU Using C#

Posted on 2014-11-10
5
Medium Priority
?
590 Views
Last Modified: 2014-11-16
I need to be able to determine the distinguished name for an Active Directory Organizational Unit (OU) using C#. Once I have it I will then be able to insert it into the below statement which is part of a program to get all groups under an OU. Can someone please provide the code to identify the distinguished name or point to a published solution? Thanks.


    PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OUDistingushedName);


Thanks in advance.
0
Comment
Question by:zonkerman
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40433620
How are you getting the OU?

An OU is by its very nature a DN. An OU can not exists that is not a full path from the root of the domain. As you must know the path to the OU to get the OU in the first place....

How are you referencing the OU in the first place? What would you pass to the function that returns the DN in your mind?
0
 
LVL 1

Author Comment

by:zonkerman
ID: 40434258
I already know the OU I need which is in the variable assignment below for OU_ToSearchForGroupsIn.  It's a somewhat lengthy string in a specific format that I understand but I don't want my user's to have to know the format.  They know the name of the OU where the groups are, which for example in the below would be "Finance Dept"   that is all I want them to have to know.   So this is why I want to know know to derive the full formatted string value based on the OU name.  Can it be done and if so how.  I wish there were a function such as the following:  
public String GetDistinguisedNameForOU(String anOU_Name);

I can already derive the Distinguished names for AD groups under an OU but for some reason the code block I currently use won't work on an OU.


try
   {
     OU_ToSearchForGroupsIn = "OU=Finance Dept,OU=Company Apps,DC=XYZCorp,DC=com
     PrincipalContext PCTX = new PrincipalContext(ContextType.Domain, DomainName, OU_ToSearchForGroupsIn);
   
    GroupPrincipal findAllGroups = new GroupPrincipal(PCTX, "*");
    PrincipalSearcher aPrincipalSearcher = new PrincipalSearcher(findAllGroups);
    foreach (var group in aPrincipalSearcher.FindAll())
    {
     try
     {
      }
    }
0
 
LVL 1

Accepted Solution

by:
zonkerman earned 0 total points
ID: 40434839
I found the answer. Basically I had to just use the DirectorySearcher with the filter shown below.  Doing this gives me the distinguished name for an OU name only.    

    String OU_Name ="Finance Dept";
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectCategory=organizationalUnit)(name=" + OU_Name + "))";

    SearchResult result = mySearcher.FindOne();

    if (result != null)
    {
       DirectoryEntry directoryObject = result.GetDirectoryEntry();
       distinguishedName = directoryObject.Properties ["distinguishedName"].Value.ToString();    
    }

This site needs more bottom line responses rather than lectures on things people are not asking for.
0
 
LVL 1

Author Closing Comment

by:zonkerman
ID: 40445501
I selected my own solution because it works and no one else provided a working solution.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40445564
Your solution is kind of OK but what happens when you have more that one OU somewhere called, for example, "Data Entry" ?

It will fail.

"This site needs more bottom line responses rather than lectures on things people are not asking for."

THAT'S why we ask questions about what you are trying to do and how you are getting there.  What this site needs is people who are willing to engage with an expert when they are asked a question. We don't ask you for more information because we have time to spare and feel like a chat, it's because we would like to provide the questioner with a foolproof, accurate solution, not something you can find on GOOGLE in five minutes that will kind of work sometimes until you put in into production and then it breaks on your users.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question