Active Directory C# help

Page 2 - Seeking answers? Join the Tom's Guide community: where nearly two million members share solutions and discuss the latest tech.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hello All,

Writting a new C# program to replace a very old VBS file. Pretty much the program will go through our AD and get all the users and their emails and put it in a CSV file. I found some basic code and ran it and I think it worked because I saw a huge list in the console but when it was finished it closed and gave me an error.

ERROR
System.ArgumentOutOfRangeException was unhandled
Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
Source="mscorlib"
ParamName="index"
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.DirectoryServices.ResultPropertyValueCollection.get_Item(Int32 index)
at ConsoleApplication4.Program.Main(String[] args) in C:\Users\jsalina\Documents\Visual Studio 2008\Projects\ConsoleApplication4\ConsoleApplication4\Program.cs:line 26
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


Code
Code:
using System;
using System.DirectoryServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://OU=PHF Users,DC=phf,DC=inc");

            using (DirectorySearcher ds = new DirectorySearcher(entry))
            {
                ds.PropertiesToLoad.Add("name");
                ds.PropertiesToLoad.Add("userPrincipalName");

                ds.Filter = "(&(objectClass=user))";

                SearchResultCollection results = ds.FindAll();

                foreach (SearchResult result in results)
                {
                    Console.WriteLine("{0} - {1}",
                        result.Properties["name"][0].ToString(),
                        result.Properties["userPrincipalName"][0].ToString());
                }
            }

        }
    }
}

Any thoughts on it?


P.S. That just the basic code, trying to get it to work in the console before I add the CSV file and validations. Pretty new to this type of programming.
 
Solution
Did you try digging into name like you did with cn? Also, double check what permissions are setup as AD may not be sending that info as you may not be allowed to see anything except names.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
heres the new code btw

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using Microsoft.Office.Interop.Excel;
using System.DirectoryServices.ActiveDirectory;


namespace EmailListing
{
    class Program
    {
        static void Main(string[] args)
        {
          

            DirectoryEntry adFolderObject = new DirectoryEntry("LDAP://OU=PHF Users,DC=phf,DC=inc");

            DirectorySearcher adSearchObject = new DirectorySearcher(adFolderObject);

            adSearchObject.SearchScope = SearchScope.Subtree;

            adSearchObject.Filter = "(&(ObjectClass=user)(!description=Built-in*))";

            foreach (SearchResult adObject in adSearchObject.FindAll())
            {
                Console.WriteLine("cn={0}", adObject.Properties["cn"][0]);
               // Console.WriteLine("co={0}", adObject.Properties["co"][0]);
               
            }

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}
 

majestic1805

Honorable
Oct 1, 2012
69
0
10,590
Did you try digging into name like you did with cn? Also, double check what permissions are setup as AD may not be sending that info as you may not be allowed to see anything except names.
 
Solution