Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
- namespace FindComputersInAD
- {
- class Program
- {
- static void Main(string[] args)
- {
- string sDomainName = @"sub.domain.com";//e.g. hk.company.com
- DirectoryEntry de = new DirectoryEntry();
- de.Path = String.Format("LDAP://{0}", sDomainName);
- de.Username = "useraccount";
- de.Password = "password";
- try
- {
- DirectorySearcher ds = new DirectorySearcher();
- ds.Filter = "(&objectCategory=computer)";
- SearchResultCollection results = ds.FindAll();
- foreach (SearchResult result in results)
- {
- ResultPropertyCollection myResultPropertyCollection = result.Properties;
- foreach (string myKey in myResultPropertyCollection.PropertyNames)
- {
- string propertyString = String.Empty;
- string tab = " ";
- Console.WriteLine("{0} = ", myKey);
- foreach (Object myCollection in myResultPropertyCollection[myKey])
- {
- Console.WriteLine("{0}{1}", tab, myCollection);
- Console.ReadKey();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement