Posts tagged ‘c#’

Getting User Information For all the users in an Active Directory Group.

A recent requirement came up for a quick bit of code to retrieve the names and e-mail addresses from an ActiveDirectroy group. I did this in code instead of power shell in order to be able to use these methods in further logic.

So here we go….

First we’re going to create a simple structure to represent the user information we need:

        public struct UserInfo
       
{
            public string DisplayName;
            public string Mail;
            public string Login;
        }

Next were going to create a simple entry point in which we will use to test our methods:

static void Main(string[] args)
{
    ArrayList ar = GetADGroupUsers("GroupName");

    List<UserInfo> users = new List<UserInfo>();
    foreach (string user in ar)
    {
        users.Add(GetADUserInfo(user));

    }
}

 

Now we will need to implement the two methods seen above. First the method to get all the users in a group:

 

static public ArrayList GetADGroupUsers(string groupName)
{
    SearchResult result;
    DirectorySearcher search = new DirectorySearcher();
    search.Filter = String.Format("(cn={0})", groupName);
    search.PropertiesToLoad.Add("member");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("samaccountname");
    result = search.FindOne();

    ArrayList userNames = new ArrayList();
    if (result != null)
    {
        for (int counter = 0; counter <
                 result.Properties["member"].Count; counter++)
        {

            userNames.Add((string)result.Properties["member"][counter]);
        }
    }
    return userNames;
}

Now that we have the ArrayList of users, we will need to get the information about the users:

static public UserInfo GetADUserInfo(string userName)
{
    SearchResult result;

    DirectoryEntry de = new DirectoryEntry("LDAP://mydomaincontroller/"+userName);

    UserInfo ui = new UserInfo();
    ui.DisplayName = de.Properties["displayName"].Value.ToString();
    ui.Mail = de.Properties["mail"].Value.ToString();
    ui.Login = de.Properties["sAMAccountName"].Value.ToString();
    return ui;

}

And that’s it, now we have a nice list of users with their email addresses.

5 Tips to a Successful Programmer Interview

  1. If you don’t know it, don’t put it on your resume.
    This is perhaps the most simple task. If you don’t embellish the technical portion of you resume you’re likely to find yourself interviewing for a position that matches your technical skills. This is critical because if you list it on your resume, and it’s a skill that they are looking for, you’re probably going to have to answer some questions about it.
  2. Confidence
    This is just too simple. Don’t push it so far as arrogance, but definitely show some confidence in yourself and in your past work.
  3. Don’t be afraid to say you don’t know.
    Nothing makes me want to end an interview faster than someone blowing smoke. If you don’t know, say so, we can move on to things you do. Also be prepared to answer, “Well since you don’t know, how would you go about learning to do that.”
  4. Be prepared to write some code.
    Although the recruiters often prepare the candidates of the fact that they are going to have to write some code. The technical interviews are very fluid and generally not written in stone. They can be adjusted to allow you to demonstrate your skill, but at the minimum you should be able to write some SQL, some data access and some basic programming such as methods, constructors, etc.  (Ask questions if you don’t understand what they are asking for…)
  5. Don’t waste their or your time.
    If you don’t think that your skills are a technical fit to the requirement, don’t bother interviewing. All your doing is wasting their time and yours.