locked
How to get Computer Name or IP in a Domain RRS feed

  • Question

  • How can I get all the computer name or IP in a domain.

    my department wants to document information on each PC's so I decided to create a program that will do just that.

    Im using WMI to get the information that is required in my report but how can I get all the IP or computer name of a computer from C#.

     

    I should be able to run the program on my computer and hunt all PC in the domain of my specification.


    The more I study, the more I realize how little I know.
    Friday, October 28, 2011 2:24 PM

Answers

  • Hello,

    You can query the Active Directory:

    using System;
    using System.DirectoryServices;
    
    namespace GetIps
    {
        internal class Program
        {
            private static void Main()
            {
                try
                {
                    var ldap = new DirectoryEntry("LDAP://yourAD", "userName", "password");
                    var searcher = new DirectorySearcher(ldap) { Filter = "(objectClass=user)" };
    
                    foreach (SearchResult result in searcher.FindAll())
                    {
                        DirectoryEntry dirEntry = result.GetDirectoryEntry();
                        // You can find the list of attributes here http://msdn.microsoft.com/en-us/library/ms675090.aspx
                        Console.WriteLine("Server name : " + dirEntry.Properties["ServerName"].Value);
                        Console.WriteLine("IP : " + dirEntry.Properties["ipHostNumber"].Value);
                        Console.WriteLine("User name : " + dirEntry.Properties["sn"].Value);
                        Console.WriteLine("Email : " + dirEntry.Properties["mail"].Value);
                        Console.WriteLine("Tel : " + dirEntry.Properties["TelephoneNumber"].Value);
                        Console.WriteLine("Description : " + dirEntry.Properties["description"].Value);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Console.Write("Press any key to continue ...");
                Console.ReadKey();
            }
        }
    }
    

    Kind regards,


    aelassas.free.fr
    • Proposed as answer by Bob Wu-MT Tuesday, November 1, 2011 6:57 AM
    • Marked as answer by Bob Wu-MT Tuesday, November 8, 2011 2:12 AM
    Friday, October 28, 2011 7:00 PM
    • Edited by smhnkmr Sunday, October 30, 2011 6:41 PM link status
    • Proposed as answer by Bob Wu-MT Tuesday, November 1, 2011 6:58 AM
    • Marked as answer by Bob Wu-MT Tuesday, November 8, 2011 2:12 AM
    Sunday, October 30, 2011 6:38 PM

All replies

  • I believe this only returns the Computer name and the IP of the hosting computer..

     

    I need to hunt all the computer in the domain, even just thier name or IP will do. But I want to do it remotely so I need a code or WQL to search all computer in the domain of my specification.


    The more I study, the more I realize how little I know.
    Friday, October 28, 2011 3:18 PM
  • If you need all computers in domain, you will have to (Depending on how it's set up) 1) Query the DNS as stated above or 2) Query Active Directory.  That's all you have... 


    JP Cowboy Coders Unite!
    Friday, October 28, 2011 3:19 PM
  • Hello,

    You can query the Active Directory:

    using System;
    using System.DirectoryServices;
    
    namespace GetIps
    {
        internal class Program
        {
            private static void Main()
            {
                try
                {
                    var ldap = new DirectoryEntry("LDAP://yourAD", "userName", "password");
                    var searcher = new DirectorySearcher(ldap) { Filter = "(objectClass=user)" };
    
                    foreach (SearchResult result in searcher.FindAll())
                    {
                        DirectoryEntry dirEntry = result.GetDirectoryEntry();
                        // You can find the list of attributes here http://msdn.microsoft.com/en-us/library/ms675090.aspx
                        Console.WriteLine("Server name : " + dirEntry.Properties["ServerName"].Value);
                        Console.WriteLine("IP : " + dirEntry.Properties["ipHostNumber"].Value);
                        Console.WriteLine("User name : " + dirEntry.Properties["sn"].Value);
                        Console.WriteLine("Email : " + dirEntry.Properties["mail"].Value);
                        Console.WriteLine("Tel : " + dirEntry.Properties["TelephoneNumber"].Value);
                        Console.WriteLine("Description : " + dirEntry.Properties["description"].Value);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Console.Write("Press any key to continue ...");
                Console.ReadKey();
            }
        }
    }
    

    Kind regards,


    aelassas.free.fr
    • Proposed as answer by Bob Wu-MT Tuesday, November 1, 2011 6:57 AM
    • Marked as answer by Bob Wu-MT Tuesday, November 8, 2011 2:12 AM
    Friday, October 28, 2011 7:00 PM
  • Does the remote PC need to have a logged in user before it can qualify to be included in the list (DirectorySearcher.FindAll)?
    The more I study, the more I realize how little I know.
    Saturday, October 29, 2011 9:06 PM
    • Edited by smhnkmr Sunday, October 30, 2011 6:41 PM link status
    • Proposed as answer by Bob Wu-MT Tuesday, November 1, 2011 6:58 AM
    • Marked as answer by Bob Wu-MT Tuesday, November 8, 2011 2:12 AM
    Sunday, October 30, 2011 6:38 PM
  • Does the remote PC need to have a logged in user before it can qualify to be included in the list (DirectorySearcher.FindAll)?
    The more I study, the more I realize how little I know.

    Hi Sheen,

    As far as I know, the remote PC don't need have a logged in user, but the remote PC should be on the same domain or same forest where many trust relationships have already been established between Active Directory instances.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Bob Wu-MT Monday, November 7, 2011 8:57 AM error type
    Monday, November 7, 2011 8:56 AM