Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Need To Determine Distinguished Name For An OU Using C#

Posted on 2014-11-10
5
411 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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