Saturday, September 11, 2010

Loading Computers from an AD OU into a C# List



This piece of code does as described: Loading Computers from an AD OU into a C# List. Often you want to put Computers into a List from a given OU, and have it recursively search through the depths of the tree. This code will let you search at the root to the various subordinate child OUs. This excerpt is from my S-Cubed Program. ("SCCM Sucks Shit!"). Screenshot above.



void loadLDAP(string OU)
        {
            try
            {
                Base = new DirectoryEntry("LDAP://" + 
                    OU);


                DirectorySearcher mySearcher = new DirectorySearcher(Base);
                mySearcher.Filter = ("(objectClass=computer)");
                foreach (SearchResult resEnt in mySearcher.FindAll())
                {
                    lstMachines.Items.Add(
               resEnt.GetDirectoryEntry().Name.ToString().
                Substring(3));
                }
            }
            catch (Exception ex)
            {
                ntyS3.ShowBalloonTip(5000, 
                    AppTitle, 
                    ex.Message, 
                    ToolTipIcon.Error);
                stsPWS.Text = "Can't contact Domain!";
                
            }
        }

No comments:

Post a Comment