473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.DirectoryServices
namespace.

DirectoryEntry dirEntry = new DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
dirSearch.Filter = "(objectClass=Computer)";
foreach (SearchResult sr in dirSearch.FindAll())
{
try
{

listBox1.Items.Add(sr.GetDirectoryEntry().Name.ToS tring().Substring(3));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
listBox1.SelectedIndex = 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=Server, 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=Server, 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 13389
"CoolBreeze812" <ls********@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.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.DirectoryServices
namespace.

DirectoryEntry dirEntry = new DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
dirSearch.Filter = "(objectClass=Computer)";
foreach (SearchResult sr in dirSearch.FindAll())
{
try
{

listBox1.Items.Add(sr.GetDirectoryEntry().Name.ToS tring().Substring(3));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
listBox1.SelectedIndex = 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=Server, 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=Server, 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 "workstation" 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 "workstations".

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.bewrote:
"CoolBreeze812" <lspence...@gmail.comwrote in messagenews:11**********************@16g2000cwy.go oglegroups.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.DirectoryServices
namespace.
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
dirSearch.Filter = "(objectClass=Computer)";
foreach (SearchResult sr in dirSearch.FindAll())
{
try
{
listBox1.Items.Add(sr.GetDirectoryEntry().Name.ToS tring().Substring(3));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
listBox1.SelectedIndex = 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=Server, 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=Server, 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 "workstation" 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 "workstations".

Willy.
Dec 10 '06 #3
"CoolBreeze812" <ls********@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.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(DirectoryEntry comp in computers.Children)
{
Console.WriteLine(comp.Properties["operatingsystem"].Value);
}
}

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

another option is to get the operatingsystemversion 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.bewrote:
"CoolBreeze812" <lspence...@gmail.comwrote in messagenews:11**********************@l12g2000cwl.g ooglegroups.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(DirectoryEntry comp in computers.Children)
{
Console.WriteLine(comp.Properties["operatingsystem"].Value);
}
}

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

another option is to get the operatingsystemversion 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_Load(object sender, EventArgs e)
{
int versionCompareResult;

DirectoryEntry dirEntry = new
DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
ManagementObjectSearcher serverQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection serverQueryCollection =
serverQuery.Get();
dirSearch.Filter = "(objectClass=computer)";

foreach (ManagementObject mo in serverQueryCollection)
{
versionCompareResult =
String.Compare(mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompareResult 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindAll())
{

computersListBox.Items.Add(sr.GetDirectoryEntry(). Name.ToString().Substring(3));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
computersListBox.SelectedIndex = 0;
}
}
}
On Dec 10, 11:21 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"CoolBreeze812" <lspence...@gmail.comwrote in messagenews:11**********************@l12g2000cwl.g ooglegroups.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(DirectoryEntry comp in computers.Children)
{
Console.WriteLine(comp.Properties["operatingsystem"].Value);
}
}

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

another option is to get the operatingsystemversion 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
"CoolBreeze812" <ls********@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.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_Load(object sender, EventArgs e)
{
int versionCompareResult;

DirectoryEntry dirEntry = new
DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
ManagementObjectSearcher serverQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection serverQueryCollection =
serverQuery.Get();
dirSearch.Filter = "(objectClass=computer)";

foreach (ManagementObject mo in serverQueryCollection)
{
versionCompareResult =
String.Compare(mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompareResult 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindAll())
{

computersListBox.Items.Add(sr.GetDirectoryEntry(). Name.ToString().Substring(3));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
computersListBox.SelectedIndex = 0;
}
}
}


You don't have to use System.Management 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");
{
DirectorySearcher dirSearch = new DirectorySearcher(root);
dirSearch.Filter = "(objectClass=Computer)";
foreach (SearchResult sr in dirSearch.FindAll())
{
// enumerate each computer entry in the directory.
using (DirectoryEntry computer = sr.GetDirectoryEntry())
{
// fetch osversion
string version =
computer.Properties["operatingsystemversion"].Value.Substring(0, 3);
switch (version)
{
case "4.0":
// NT4 Server or WKST, here you'll have to parse
"operatingsystem" property !
// fill list ...
break;
case "5.0":
// Windows 2000 Server or WKST, here you'll have to parse
"operatingsystem" 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.bewrote:
"CoolBreeze812" <lspence...@gmail.comwrote in messagenews:11**********************@j44g2000cwa.g ooglegroups.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_Load(object sender, EventArgs e)
{
int versionCompareResult;
DirectoryEntry dirEntry = new
DirectoryEntry("LDAP://spencenet.com");
DirectorySearcher dirSearch = new
DirectorySearcher(dirEntry);
ManagementObjectSearcher serverQuery = new
ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
ManagementObjectCollection serverQueryCollection =
serverQuery.Get();
dirSearch.Filter = "(objectClass=computer)";
foreach (ManagementObject mo in serverQueryCollection)
{
versionCompareResult =
String.Compare(mo["version"].ToString(), "5.1 (2600)");
mo["version"].ToString();
if (versionCompareResult 0)
{
try
{
foreach (SearchResult sr in
dirSearch.FindAll())
{
computersListBox.Items.Add(sr.GetDirectoryEntry(). Name.ToString().Substring(3));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LDAP: Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
computersListBox.SelectedIndex = 0;
}
}
}You don't have to use System.Management 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");
{
DirectorySearcher dirSearch = new DirectorySearcher(root);
dirSearch.Filter = "(objectClass=Computer)";
foreach (SearchResult sr in dirSearch.FindAll())
{
// enumerate each computer entry in the directory.
using (DirectoryEntry computer = sr.GetDirectoryEntry())
{
// fetch osversion
string version =
computer.Properties["operatingsystemversion"].Value.Substring(0, 3);
switch (version)
{
case "4.0":
// NT4 Server or WKST, here you'll have to parse
"operatingsystem" property !
// fill list ...
break;
case "5.0":
// Windows 2000 Server or WKST, here you'll have to parse
"operatingsystem" 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
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...
99
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...
1
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...
2
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...
0
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...
0
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...
1
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...
10
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...
5
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.