CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Sep 2002
    Location
    Mumbai
    Posts
    30

    Question all computer names in a domain

    hello,
    Does Anyone know how to get all computer names in a domain

    Thanx
    Amit

  2. #2
    Join Date
    Aug 2002
    Location
    Redmond, WA, USA
    Posts
    88
    Here is a sample snippet that will hopefully accomplish what you are trying to do.

    Note: I wrote this sample from scratch and did not have a domain to test it against. You will need to modify the LDAP string passed to the DirectoryEntry constructor to point to the root of the domain you want to search. Other than that, this should work for you.

    You will need to add a reference to System.DirectoryServices.DLL from Projects... References. You will also have to add to the top of your file that uses this routing the following line:

    uses System.DirectoryServices;

    <--- Snippet starts --->

    DirectoryEntry oRoot = new DirectoryEntry("LDAP://DC=mysubdomain,DC=mydomain,DC=com");

    DirectorySearcher oSearcher = new DirectorySearcher(oRoot);

    oSearcher.Filter = "(objectClass=computer)";
    oSearcher.PropertiesToLoad.Add("cn");

    SearchResultCollection oResults = oSearcher.FindAll();

    foreach (SearchResult oResult in oResults)
    {
    listBox1.Items.Add(oResult.Properties["cn"].ToString());
    }

    <--- Snippet ends --->


    Hope this helps!

    - Robert

  3. #3
    Join Date
    Sep 2002
    Location
    Mumbai
    Posts
    30
    Thnx Robert
    can u help me with the provider = WinNT://

    Regds
    Amit

  4. #4
    Join Date
    Aug 2002
    Location
    Redmond, WA, USA
    Posts
    88
    Since the WinNT provider does not support "searching" you must do the work yourself. Here is a quick example.


    <<<--- Code Snippet Start --->>>

    private void SearchTree(DirectoryEntry oTree)
    {
    if ( 0 == String.Compare(oTree.SchemaClassName, "computer", true) )
    listBox1.Items.Add( oTree.Name );

    foreach (DirectoryEntry oChild in oTree.Children)
    {
    SearchTree(oChild);
    }
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    DirectoryEntry oRoot = new DirectoryEntry("WinNT://dsaddom");

    SearchTree(oRoot);
    }
    catch (Exception ex)
    {
    listBox1.Items.Add( ex.Message );
    }
    }

    <<<--- Code Snippet End --->>>



    Hope that helps.

    - Robert

  5. #5
    Join Date
    Oct 2004
    Posts
    9

    Question Re: all computer names in a domain

    I have looked over this and over this and in all other threads but did not find any answers for me. I do want to get the computer names (and IP addresses) on the domain BUt, the code does not seem to work for me.

    When running I get an error and this pops up:
    at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
    at System.DirectoryServices.DirectoryEntry.Bind()
    at System.DirectoryServices.DirectoryEntry.get_AdsObject()
    at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
    at System.DirectoryServices.DirectorySearcher.FindAll()
    at Class1.Main(String[] args)

    Anyone have an idea or have better exact code or even just some hints would be great help!!

    Thank you.

  6. #6
    Join Date
    Aug 2004
    Location
    USA
    Posts
    90

    Re: all computer names in a domain

    try doing this as well

    WindowsIdentity MyIdentity = WindowsIdentity.GetCurrent();
    WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
    string name = MyPrincipal.Identity.Name;
    string Type = MyPrincipal.Identity.AuthenticationType;
    string Auth = MyPrincipal.Identity.IsAuthenticated.ToString();


    here string name gives which domain you belong to..I hope this will help you
    - Shash

  7. #7
    Join Date
    Oct 2004
    Posts
    9

    Cool Re: all computer names in a domain

    How is that going to help me??? I already know what domain I am in. This is the code I am using:

    using System;
    using System.DirectoryServices;

    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    DirectoryEntry entry = new DirectoryEntry("zeltech.com");
    DirectorySearcher searcher = new DirectorySearcher(entry);

    searcher.Filter = "(objectClass=computer)";

    foreach (SearchResult res in searcher.FindAll())
    {
    Console.WriteLine(res.GetDirectoryEntry().Name.ToString());
    }
    }
    }

    It compiles but when I run it I get the error above (in the other part of this conversation).

    Any ideas!?

  8. #8

    Re: all computer names in a domain

    Try changing your DirectoryEntry constructor to include the proper provider. In other words, if it's a domain, try LDAP://zeltech.com.

    Another tip is you might want to be a little more polite with regard to people's suggestions.

  9. #9
    Join Date
    Oct 2004
    Posts
    9

    Red face Re: all computer names in a domain

    I am sorry about being "rude" about someones suggestion. I did not mean to be, I do thank everyone for their help.

    I have also tried the LDAP:// thing and it did not seem to work either???

  10. #10
    Join Date
    Oct 2004
    Posts
    9

    Cool Re: all computer names in a domain

    Okay, for the windows identity stuff, what do I need to "use" (using System) in the file to get that to work and any others needed for that code? I am really new to c sharp!

    Thank you!!

  11. #11
    Join Date
    Oct 2004
    Posts
    9

    Cool Re: all computer names in a domain

    I found the correct System directives that I needed to use. But, I am still having problems with the LDAP, what are the other choices? What does LDAP stand for?? Thank you.
    Last edited by bluid; October 29th, 2004 at 08:41 AM.

  12. #12
    Join Date
    Oct 2004
    Posts
    9

    Red face Re: all computer names in a domain

    Anyone want to help me out? I am still having some problems and it is really making me crazy. I appreciate any help that I can get. Thank you.

  13. #13
    Join Date
    Oct 2004
    Posts
    29

    Exclamation Re: all computer names in a domain

    LDAP stands for Lightweight Directory Access Protocol.

    Read this page for help:
    http://msdn.microsoft.com/library/de...dingADApps.asp

  14. #14
    Join Date
    Oct 2004
    Posts
    9

    Question Re: all computer names in a domain

    Thank you for that info. It was very informative. But I still have a question as to why it is tanking when doing the FindAll function???

  15. #15
    Join Date
    Oct 2004
    Posts
    9

    Question Re: all computer names in a domain

    Anyone, anyone?? I really need some help. I can not seem to find anything else but this code for this and I have tried so many different ways and can not get anything to go past the FindAll method (see above).

    Any help would be greatly appreciated!! Thank you.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured