Click to See Complete Forum and Search --> : all computer names in a domain


asaxena11
October 29th, 2002, 02:24 AM
hello,
Does Anyone know how to get all computer names in a domain

Thanx
Amit

RobAnd
November 7th, 2002, 08:52 PM
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

asaxena11
November 14th, 2002, 09:55 AM
Thnx Robert
can u help me with the provider = WinNT://

Regds
Amit

RobAnd
November 14th, 2002, 05:10 PM
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

bluid
October 27th, 2004, 12:34 PM
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.

shash
October 27th, 2004, 01:11 PM
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

bluid
October 27th, 2004, 02:32 PM
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!?

mmetzger
October 27th, 2004, 04:29 PM
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.

bluid
October 29th, 2004, 05:15 AM
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???

bluid
October 29th, 2004, 07:09 AM
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!! :)

bluid
October 29th, 2004, 08:09 AM
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.

bluid
November 3rd, 2004, 07:07 AM
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.

Jedimark
November 3rd, 2004, 08:26 AM
LDAP stands for Lightweight Directory Access Protocol.

Read this page for help:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnactdir/html/BuildingADApps.asp

bluid
November 3rd, 2004, 09:03 AM
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???

bluid
November 4th, 2004, 12:34 PM
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.

T_M_P
November 5th, 2004, 05:56 AM
I think you have to try to use LDAP in the DirectoryEntry constructor and also the user and password of an authorized accont to search in AD (I'm writing an app using AD and everythings works well)

ForExample:


DirectoryEntry entry = new DirectoryEntry("LDAP://OU=MyOu,DC=zeltech,DC=com",zeltech,<myaccount>,<mypsw>);

DirectorySearcher searcher = new DirectorySearcher(entry,"(&(ObjectCategory=computer)");

SearchResultCollection res = searcher.FindAll();



I hope this can be useful for you..

bluid
November 9th, 2004, 03:56 PM
Well, I was doing some research and the conclusion I came up with is that we need to have active directory at least installed on the computer, right???