Listing Machines on a Domain

  • Thread starter Thread starter Jonny
  • Start date Start date
J

Jonny

I'm sure this is easy and i'll kick myself for it.

How would i go about listing all the machines on a domain?
I want to display them in a list box.

Thanks

/Jonny
 
Jonny,

You will have to make a call to the NetServerEnum API function through
the P/Invoke layer. This will allow you to get the names of different types
of machines (including all machines, if you wish) in your domain.

Hope this helps.
 
use DirectoryService:
DirectoryEntry de = new DirectoryEntry
("WinNT://YourDomain");
DirectoryEntries des = deDomain.Children;
foreach (DirectoryEntry d in desDomain) {
if ("Computer" == d.SchemaClassName) { //do something }
}


Linh Nguyen
}
 
Thanks, A few changes and the code was great.

It does throw up absolutely every machine ever on the
domain... is there a way to filter the list so that only
active or current machines are shown?

Thanks again guys,

/Jonny



-----Original Message-----
Jonny,

You will have to make a call to the NetServerEnum API function through
the P/Invoke layer. This will allow you to get the names of different types
of machines (including all machines, if you wish) in your domain.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm sure this is easy and i'll kick myself for it.

How would i go about listing all the machines on a domain?
I want to display them in a list box.

Thanks

/Jonny


.
 
Don't PInvoke NetXXXX API's, use DirectoryServices namespace instead.

Try this:

using System;
using System.DirectoryServices;
//****************************************************************************/

class Tester {
public static void Main() {
String unl = new string(('_'),60);

DirectoryEntry container;
using( container = new DirectoryEntry("WinNT://celeb", "celeb\\administrator", "kevin", AuthenticationTypes.ServerBind))
{
DirectoryEntries computers = container.Children;
computers.SchemaFilter.Add("Computer");
foreach (DirectoryEntry computer in computers) {
try
{
Console.WriteLine("domain member path: " + computer.Path);
// connect with the member to retrieve its name
// this can be very time consuming in a large domain, especially when some PC's are off-line
Console.WriteLine("Name" + ":\t" + (computer.Properties["Name"])[0].ToString());
}
catch(Exception e)
{
// "The network path was not found." will be thrown when a member is down or unreacheable.
Console.WriteLine(e.Message );
}
}
}
}
}

Willy.
 
Back
Top