如何查詢 Windows Active Directory Groups 成員


  1. 說明
    1. 使用網路 / 搜尋 Active Directory
    2. Sysinternals AD Explorer
    3. .NET Framework
    4. PowerShell
  2. 參考資料

筆記查詢 Windows AD Groups Members 的各種方式。

logo

說明

使用網路 / 搜尋 Active Directory

原生於 Windows 10 作業系統之中,缺點是查詢的控制項目有限。

Sysinternals AD Explorer

Sysinternals 中輕量的工具,易於從 AD 中進行各種面向的查詢。

AD Explorer

.NET Framework

使用 System.DirectoryServices 向 LDAP Service 來做查詢。

using System.DirectoryServices;

    public class ADUserQuery
    {
        public string Description { get; set; }
        public string Name { get; set; }
        public DateTime BadWordTimestamp{ get; set; }
        public DateTime LastLogonTimestamp{ get; set; }
        public string Result { get; set; }
    }

    public static ADUserQuery GetEmpAdgroup(string empId)
    {
        var adgroup = new ADUserQuery();
        adgroup.Result = "";
        Dictionary<string, string> result = new Dictionary<string, string> { };
        if (empId != null && empId != "" && empId.Length <= 6)
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://domain.com.tw");
            DirectorySearcher Dsearch = new DirectorySearcher(entry);
            Dsearch.Filter = "(&(objectClass=user)(objectClass=person)(sAMAccountName=" + empId + "))";
            foreach (SearchResult sResultSet in Dsearch.FindAll())
            {
                if (sResultSet.Properties["memberOf"].Count > 0)
                {
                    var rs = "";
                    foreach (var s in sResultSet.Properties["memberOf"])
                    {
                        rs += s.ToString().Split(',')[0] + "<br/>";
                    }
                    adgroup.Result = rs;
                }
                adgroup.Description = (string)sResultSet.Properties["description"][0];
                adgroup.Name = (string)sResultSet.Properties["displayName"][0];
            }
        }
        return adgroup;
    }

PowerShell

藉由安裝 RAST 以取得使用 Powershell Active Directory Modules 的能力。

參考資料

安裝POWERSHELL - ACTIVE DIRECTORY MODULE解決AD大小事