Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need To Determine Distinguished Name For An OU Using C#

Posted on 2014-11-10
5
Medium Priority
?
546 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
[X]
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
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

671 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