Reputation: 867
I want to get all client-only computer objects from AD, I mean, Windows Clients machines. I am using the sample code from this thread, but I would like to know how I can filter servers out.
Here's my code:
class Program
{
public static List<string> GetComputers()
{
List<string> ComputerNames = new List<string>();
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://neuventus.local");
DirectorySearcher mySearcher = new DirectorySearcher(dirEntry);
mySearcher.Filter = ("(objectClass=computer)");
mySearcher.SizeLimit = int.MaxValue;
mySearcher.PageSize = int.MaxValue;
foreach (SearchResult resEnt in mySearcher.FindAll())
{
string ComputerName = resEnt.GetDirectoryEntry().Name;
if (ComputerName.StartsWith("CN="))
ComputerName = ComputerName.Remove(0, "CN=".Length);
ComputerNames.Add(ComputerName);
}
mySearcher.Dispose();
dirEntry.Dispose();
return ComputerNames;
}
static void Main(string[] args)
{
List<string> newList = GetComputers();
newList.ForEach(Console.WriteLine);
System.IO.File.WriteAllLines("C:\\SavedLists.txt", newList);
}
}
Can anyone help me?
Upvotes: 0
Views: 174
Reputation: 2082
Try adding an operatingSystem
condition to your filter.
mySearcher.Filter = "(&(objectClass=computer)(!(operatingSystem=*server*)))";
Note that this will only work if the all your servers are running a server operating system with server
in the name. Also assume that no client machines run a server operating system.
Upvotes: 1