473,616 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Howto get just servers from Active Directory

Hello,

I'm using the following code to get the list of computers joined to a
domain in Active Directory and using the System.Director yServices
namespace.

DirectoryEntry dirEntry = new DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
dirSearch.Filte r = "(objectClass=C omputer)";
foreach (SearchResult sr in dirSearch.FindA ll())
{
try
{

listBox1.Items. Add(sr.GetDirec toryEntry().Nam e.ToString().Su bstring(3));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
listBox1.Select edIndex = 0;
}

The problem is that it returns all computers, both servers and
workstations. I would like to limit this to only servers being listed
in the listbox. I have tried objectClass=Ser ver, but nothing was
returned and I haven't been able to find any other attribute that would
list only the servers in the domain.

I have a test domain setup and tried the code both on a Windows 2003
server and an XP workstation that is joined to the domain with the
objectClass=Ser ver, both did not populate the listbox with only the
listing of the Windows 2003 server. FYI, I have the Windows 2003 server
configured with a role of Domain Controller and DNS Server only.

Does anyone know how I can retrieve only servers within an Active
Directory Domain?

Thanks,
-- Lance

Dec 10 '06 #1
7 13452
"CoolBreeze 812" <ls********@gma il.comwrote in message
news:11******** **************@ 16g2000cwy.goog legroups.com...
Hello,

I'm using the following code to get the list of computers joined to a
domain in Active Directory and using the System.Director yServices
namespace.

DirectoryEntry dirEntry = new DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
dirSearch.Filte r = "(objectClass=C omputer)";
foreach (SearchResult sr in dirSearch.FindA ll())
{
try
{

listBox1.Items. Add(sr.GetDirec toryEntry().Nam e.ToString().Su bstring(3));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
listBox1.Select edIndex = 0;
}

The problem is that it returns all computers, both servers and
workstations. I would like to limit this to only servers being listed
in the listbox. I have tried objectClass=Ser ver, but nothing was
returned and I haven't been able to find any other attribute that would
list only the servers in the domain.

I have a test domain setup and tried the code both on a Windows 2003
server and an XP workstation that is joined to the domain with the
objectClass=Ser ver, both did not populate the listbox with only the
listing of the Windows 2003 server. FYI, I have the Windows 2003 server
configured with a role of Domain Controller and DNS Server only.

Does anyone know how I can retrieve only servers within an Active
Directory Domain?

Thanks,
-- Lance
The directory only knows "computer" as object type, that means that there is no way to make
a distinction between a "workstatio n" and a "server". All you can do to make a distinction
between both is by querying for the OS version installed on the "computer" another option is
to put servers in a distinct OU from the "workstatio ns".

Willy.

Dec 10 '06 #2
Thanks for the reply. Using the code I presented, how would I query
the OS version?

-- Lance

On Dec 10, 10:28 am, "Willy Denoyette [MVP]"
<willy.denoye.. .@telenet.bewro te:
"CoolBreeze 812" <lspence...@gma il.comwrote in messagenews:11* *************** ******@16g2000c wy.googlegroups .com...
Hello,
I'm using the following code to get the list of computers joined to a
domain in Active Directory and using the System.Director yServices
namespace.
DirectoryEntry dirEntry = new DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
dirSearch.Filte r = "(objectClass=C omputer)";
foreach (SearchResult sr in dirSearch.FindA ll())
{
try
{
listBox1.Items. Add(sr.GetDirec toryEntry().Nam e.ToString().Su bstring(3));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
listBox1.Select edIndex = 0;
}
The problem is that it returns all computers, both servers and
workstations. I would like to limit this to only servers being listed
in the listbox. I have tried objectClass=Ser ver, but nothing was
returned and I haven't been able to find any other attribute that would
list only the servers in the domain.
I have a test domain setup and tried the code both on a Windows 2003
server and an XP workstation that is joined to the domain with the
objectClass=Ser ver, both did not populate the listbox with only the
listing of the Windows 2003 server. FYI, I have the Windows 2003 server
configured with a role of Domain Controller and DNS Server only.
Does anyone know how I can retrieve only servers within an Active
Directory Domain?
Thanks,
-- LanceThe directory only knows "computer" as object type, that means that there is no way to make
a distinction between a "workstatio n" and a "server". All you can do to make a distinction
between both is by querying for the OS version installed on the "computer" another option is
to put servers in a distinct OU from the "workstatio ns".

Willy.
Dec 10 '06 #3
"CoolBreeze 812" <ls********@gma il.comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Thanks for the reply. Using the code I presented, how would I query
the OS version?

-- Lance
Supposed all computers are contained in computers (as per default), you can simply do
something like this:

// bind to and get at the "computers" container
using (DirectoryEntry computers = new
DirectoryEntry( "LDAP://spencenet.com/cn=computers");
{
foreach(Directo ryEntry comp in computers.Child ren)
{
Console.WriteLi ne(comp.Propert ies["operatingsyste m"].Value);
}
}

this should return something like:
Windows Server 2003
Windows XP Professional
Windows VistaT Ultimate

another option is to get the operatingsystem version property, this will return something
like:
5.2 (3790)
5.1 (2600)
6.0 (6000)

obviously you'l have to parse the string to determine the computer type, in which case it's
preferable to parse the version string.

Willy.

Dec 10 '06 #4
Again thank you very much for your help.
-- Lance

On Dec 10, 11:21 am, "Willy Denoyette [MVP]"
<willy.denoye.. .@telenet.bewro te:
"CoolBreeze 812" <lspence...@gma il.comwrote in messagenews:11* *************** ******@l12g2000 cwl.googlegroup s.com...
Thanks for the reply. Using the code I presented, how would I query
the OS version?
-- LanceSupposed all computers are contained in computers (as per default), you can simply do
something like this:

// bind to and get at the "computers" container
using (DirectoryEntry computers = new
DirectoryEntry( "LDAP://spencenet.com/cn=computers");
{
foreach(Directo ryEntry comp in computers.Child ren)
{
Console.WriteLi ne(comp.Propert ies["operatingsyste m"].Value);
}
}

this should return something like:
Windows Server 2003
Windows XP Professional
Windows VistaT Ultimate

another option is to get the operatingsystem version property, this will return something
like:
5.2 (3790)
5.1 (2600)
6.0 (6000)

obviously you'l have to parse the string to determine the computer type, in which case it's
preferable to parse the version string.

Willy.
Dec 10 '06 #5
Again thank you Willy for your help. I tried your example and
unfortunately couldn't get it to work. What I'd really like to do is
adjust the following code to accomplish what I'm after, but I am having
a hard time doing so. Do you have any pointers on how I might be able
to do this. As you can see I'm a newbie, so I welcome any refactoring
suggestions as well.

private void wsrMainForm_Loa d(object sender, EventArgs e)
{
int versionCompareR esult;

DirectoryEntry dirEntry = new
DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
ManagementObjec tSearcher serverQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
ManagementObjec tCollection serverQueryColl ection =
serverQuery.Get ();
dirSearch.Filte r = "(objectClass=c omputer)";

foreach (ManagementObje ct mo in serverQueryColl ection)
{
versionCompareR esult =
String.Compare( mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompare Result 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindA ll())
{

computersListBo x.Items.Add(sr. GetDirectoryEnt ry().Name.ToStr ing().Substring (3));
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
computersListBo x.SelectedIndex = 0;
}
}
}
On Dec 10, 11:21 am, "Willy Denoyette [MVP]"
<willy.denoye.. .@telenet.bewro te:
"CoolBreeze 812" <lspence...@gma il.comwrote in messagenews:11* *************** ******@l12g2000 cwl.googlegroup s.com...
Thanks for the reply. Using the code I presented, how would I query
the OS version?
-- LanceSupposed all computers are contained in computers (as per default), you can simply do
something like this:

// bind to and get at the "computers" container
using (DirectoryEntry computers = new
DirectoryEntry( "LDAP://spencenet.com/cn=computers");
{
foreach(Directo ryEntry comp in computers.Child ren)
{
Console.WriteLi ne(comp.Propert ies["operatingsyste m"].Value);
}
}

this should return something like:
Windows Server 2003
Windows XP Professional
Windows VistaT Ultimate

another option is to get the operatingsystem version property, this will return something
like:
5.2 (3790)
5.1 (2600)
6.0 (6000)

obviously you'l have to parse the string to determine the computer type, in which case it's
preferable to parse the version string.

Willy.
Dec 10 '06 #6
"CoolBreeze 812" <ls********@gma il.comwrote in message
news:11******** **************@ j44g2000cwa.goo glegroups.com.. .
Again thank you Willy for your help. I tried your example and
unfortunately couldn't get it to work. What I'd really like to do is
adjust the following code to accomplish what I'm after, but I am having
a hard time doing so. Do you have any pointers on how I might be able
to do this. As you can see I'm a newbie, so I welcome any refactoring
suggestions as well.

private void wsrMainForm_Loa d(object sender, EventArgs e)
{
int versionCompareR esult;

DirectoryEntry dirEntry = new
DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
ManagementObjec tSearcher serverQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
ManagementObjec tCollection serverQueryColl ection =
serverQuery.Get ();
dirSearch.Filte r = "(objectClass=c omputer)";

foreach (ManagementObje ct mo in serverQueryColl ection)
{
versionCompareR esult =
String.Compare( mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompare Result 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindA ll())
{

computersListBo x.Items.Add(sr. GetDirectoryEnt ry().Name.ToStr ing().Substring (3));
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
computersListBo x.SelectedIndex = 0;
}
}
}


You don't have to use System.Manageme nt to get at the OS version, this is all stored in the
AD.

You ca do something like this, if you like to bind to the root of the AD....
...
using (DirectoryEntry root = new DirectoryEntry( "LDAP://spencenet.com") ;
{
DirectorySearch er dirSearch = new DirectorySearch er(root);
dirSearch.Filte r = "(objectClass=C omputer)";
foreach (SearchResult sr in dirSearch.FindA ll())
{
// enumerate each computer entry in the directory.
using (DirectoryEntry computer = sr.GetDirectory Entry())
{
// fetch osversion
string version =
computer.Proper ties["operatingsyste mversion"].Value.Substrin g(0, 3);
switch (version)
{
case "4.0":
// NT4 Server or WKST, here you'll have to parse
"operatingsyste m" property !
// fill list ...
break;
case "5.0":
// Windows 2000 Server or WKST, here you'll have to parse
"operatingsyste m" property !
...
break;
case "5.1":
// Windows XP, this is a WKST OS
...
break;
case "5.2":
// Windows 2003 Server, this is a Server OS
...
break;
case "6.0":
// Vista, this is a WKST OS
...
break;
default:
// Unknown
break;

}
}
}
}

But, it's faster to bind to the root of the container if possible, for instance the
"computers" container contains all computers in a domain (default), you can change the code
into this:

using (DirectoryEntry root = new DirectoryEntry( "LDAP://spencenet.com, cn=computers");
{
..

Willy.

Dec 10 '06 #7
Thanks for the sample. I believe I'm on the right track now.

-- Lance

On Dec 10, 3:30 pm, "Willy Denoyette [MVP]"
<willy.denoye.. .@telenet.bewro te:
"CoolBreeze 812" <lspence...@gma il.comwrote in messagenews:11* *************** ******@j44g2000 cwa.googlegroup s.com...
Again thank you Willy for your help. I tried your example and
unfortunately couldn't get it to work. What I'd really like to do is
adjust the following code to accomplish what I'm after, but I am having
a hard time doing so. Do you have any pointers on how I might be able
to do this. As you can see I'm a newbie, so I welcome any refactoring
suggestions as well.
private void wsrMainForm_Loa d(object sender, EventArgs e)
{
int versionCompareR esult;
DirectoryEntry dirEntry = new
DirectoryEntry( "LDAP://spencenet.com") ;
DirectorySearch er dirSearch = new
DirectorySearch er(dirEntry);
ManagementObjec tSearcher serverQuery = new
ManagementObjec tSearcher("SELE CT * FROM Win32_Operating System");
ManagementObjec tCollection serverQueryColl ection =
serverQuery.Get ();
dirSearch.Filte r = "(objectClass=c omputer)";
foreach (ManagementObje ct mo in serverQueryColl ection)
{
versionCompareR esult =
String.Compare( mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompare Result 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindA ll())
{
computersListBo x.Items.Add(sr. GetDirectoryEnt ry().Name.ToStr ing().Substring (3));
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message, "LDAP: Error",
MessageBoxButto ns.OK, MessageBoxIcon. Error);
}
computersListBo x.SelectedIndex = 0;
}
}
}You don't have to use System.Manageme nt to get at the OS version, this is all stored in the
AD.

You ca do something like this, if you like to bind to the root of the AD....
...
using (DirectoryEntry root = new DirectoryEntry( "LDAP://spencenet.com") ;
{
DirectorySearch er dirSearch = new DirectorySearch er(root);
dirSearch.Filte r = "(objectClass=C omputer)";
foreach (SearchResult sr in dirSearch.FindA ll())
{
// enumerate each computer entry in the directory.
using (DirectoryEntry computer = sr.GetDirectory Entry())
{
// fetch osversion
string version =
computer.Proper ties["operatingsyste mversion"].Value.Substrin g(0, 3);
switch (version)
{
case "4.0":
// NT4 Server or WKST, here you'll have to parse
"operatingsyste m" property !
// fill list ...
break;
case "5.0":
// Windows 2000 Server or WKST, here you'll have to parse
"operatingsyste m" property !
...
break;
case "5.1":
// Windows XP, this is a WKST OS
...
break;
case "5.2":
// Windows 2003 Server, this is a Server OS
...
break;
case "6.0":
// Vista, this is a WKST OS
...
break;
default:
// Unknown
break;

}
}
}
}

But, it's faster to bind to the root of the container if possible, for instance the
"computers" container contains all computers in a domain (default), you can change the code
into this:

using (DirectoryEntry root = new DirectoryEntry( "LDAP://spencenet.com, cn=computers");
{
..

Willy.
Dec 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3717
by: Mario Rodriguez | last post by:
Hi people. I have a problem adding users to Win2003 active directory programatically. When I execute my app throws the following exception: .................The specified directory service attribute or value does not exist........... Exactly the same code works fine on my win2000 active directory. My app include the use of the extensionAtributes and I'm not sure if the extensionAttributes feature was removed from win2003 Active...
99
6068
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
1
1619
by: STeve | last post by:
Hi guys, In my active directory class I was wondering how I would be able to catch invalid UserID's entered by the user. For example say if the user entered in a UserID as a. (well any set of characters followed by a period), an error gets thrown: A device attached to the system is not functioning... etc I was wondering if there is a way to catch this error (instead of just
2
3198
by: Dan | last post by:
hi ng, is there a way to set a page the user gets redirected when using windows authentification (and the user gets authentificated by active directory) and authorization failed?) i have tried to set the iis error page and the customerrors at web.config, none of them worked. when the user cannot be authenticated (e.g. because password wrong), iis uses the custom error page i defined for the application. as i wanted to. but when...
0
1202
by: Martijn | last post by:
Hello, For those who are interested in creating Account in Active Directory here is some code I used. ublic Function create_user(ByVal firstname As String, ByVal lastname As String, _ ByVal Address As String, ByVal pwd As String) Try Dim MyRoot As DirectoryServices.DirectoryEntry
0
966
by: Brave | last post by:
Hello, I have sometimes the problem to get information from Active Directory (100 times it works without any problems but the next time I had the problem.) If I copy the Web-application on the single server It works without any problem. The problem is definitely a cluster problem. (On each IIS itself it works, but when I go over the cluster-IP I get sometimes the error.
1
2984
by: kurt sune | last post by:
I am having trouble publishing a website for RELEASE. 1. web.config: <compilation defaultLanguage="vb" debug="false"> 2. in Configuration manager I set the configuration to Release for the website 3. in Configuration manager I set the Active solution configuration to Release
10
2088
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I am trying to setup ASPNET account for .NET application to access database (SQL Server 2000) by aspnet_regiis -i in the directory C:\WINNT\Microsoft.NET\Framework\v1.1.4322 After running it, I got a successful installed information. But I can not find this account in Active Directory. The authentication uses Active Directory. David
5
2673
by: plomon | last post by:
I have this class project which requires me to demonstrate service availability using active and backup servers with the following conditions and requirements: A Name Server (say NS) to keep track of all the servers. One TCP connection between each client and the server. There will be one active server (say AS) and two backup servers (say BS1 and BS2). Heartbeat between the active server and client. If AS fails, BS1 should handle the...
0
8146
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8592
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.