Tuesday, April 20, 2010

Getting Computer Names from Domain using C#

ArrayList ALExist = new ArrayList();
ArrayList AL = new ArrayList();
DataTable dt_Domainsystem_withIP = new DataTable();



public void get_computername()
{
   //Invoke(new MyDelegate(ShowProgressBar), false);
   //string server = ""; //Insert Domain controller server here
   string server = Environment.UserDomainName; //Insert Domain controller server here
   string adminUser = Environment.UserDomainName + Environment.UserName;//Insert an admin user account here
   //string adminUser = ""; 
    string adminPass = ""; //Password for above username
    DirectoryEntry de = new DirectoryEntry();
    de.Path = "LDAP://" + server + "";
    de.Username = adminUser;
    de.Password = adminPass;
            

    try
    {
                float progress = 0;
                DirectorySearcher ser = new DirectorySearcher();
                ser.Filter = "(&ObjectCategory=computer)"; //Only allows Computers to be returned in results.
                SearchResultCollection results = ser.FindAll();
                foreach (SearchResult res in results)
                {
                    string[] temp = res.Path.Split(','); //temp[0] would contain the computer name ex: cn=computerName,..
                    AL.Add(temp[0].Substring(10).ToString());//returns everything after LDAP://CN= until end of temp[0].
                }
                //c1_doaminSystems.DataSource = AL;
                //c1_doaminSystems.Rebind(true);
                //DataSet ds = new DataSet();

                AL.Sort();
                lbl_total_domain.Text = AL.Count.ToString();
                
                try
                {
                    dt_Domainsystem_withIP.Columns.Add("Computer Name", typeof(string));
                    dt_Domainsystem_withIP.Columns.Add("IP Address", typeof(string));
                }
                catch (Exception ex)
                { }



                for (int i = 0; i < AL.Count; i++)
                {

                    dr = dt_Domainsystem_withIP.NewRow();
                    dr[0] = AL[i].ToString();

                    try
                    {
                        IPAddress[] addresslist = Dns.GetHostAddresses(AL[i].ToString());
                        foreach (IPAddress theaddress in addresslist)
                        {
                            dr[1] = theaddress.ToString();
                        }
                        ALExist.Add(AL[i].ToString());
                    }
                    catch (Exception ex)
                    {
                        //ALIPException.Add(AL[i].ToString());
                        dr[1] = ex.Message.ToString();
                    }


                    dt_Domainsystem_withIP.Rows.Add(dr);
                    float divide = (float) i / AL.Count;
                    progress = (divide*100);

                    int total_progess = (int)progress;

                    if (total_progess % 2 == 0)
                    {
                        lbl_progress.Text = total_progess.ToString() + "% Complete..";
                    }
                    else
                        lbl_progress.Text = total_progess.ToString() + "% Complete....";


                }
                Grid.DataSource = dt_Domainsystem_withIP;
                Grid.Rebind(true);
                
                


                








            }

            catch (Exception ex)
            {

               // Console.WriteLine(ex.ToString());

            }

            finally
            {

                de.Dispose();//Clean up resources

            }



        }

No comments:

Post a Comment