2009-06-28 69 views

回答

6

假設您只想獲取返回Department屬性的對象列表,則可以在System.DirectoryServices命名空間中使用DirectorySearcher。

那麼你的過濾器會是這樣:

ds.Filter = "(objectClass=user)"; 

,然後你可以告訴搜索者只需加載部門屬性:

ds.PropertiesToLoad.Add("department"); 

然後枚舉throught結果集:

SearchResultCollection results = ds.FindAll(); 

然後將每個部門屬性添加到詞典以獲取所有唯一值

foreach (SearchResult result in results) 
{ 
    string dept = String.Empty; 
    DirectoryEntry de = result.GetDirectoryEntry(); 
    if (de.Properties.Contains("department")) 
    { 
    dept = de.Properties["department"][0].ToString(); 
    if (!dict.ContainsKey(dept)) 
    { 
     dict.Add(result.Properties["department"][0].ToString(); 
    } 
    } 
} 


另外,還有一些命令行工具,這將給你這個信息,如DSQUERY或adfind時。

adfind -default -f "(objectclass=user)" department -list | sort 

將爲您提供所有用戶的部門屬性的排序列表。

+0

adfind是用於Windows操作系統的'遠程服務器管理工​​具(RSAT)'的一部分'https://support.microsoft.com/en-us/kb/2693643。免費軟件提供這些工具的改進版本http://www.joeware.net/freetools/ – BiLaL 2015-11-21 08:30:01

相關問題