日期:2014-05-18  浏览次数:20362 次

Active Directory把Group导入到Excel的问题
是用Winform做的一个Tool左边的Treeview是从AD上面拉下来的OU&Group.我把OU或者Group拉到右边时是一个Gridview显示该OU或者Group的信息都没问题.但是ParentGroups在Group有嵌套关系时居然获取的是Group自身的ID.ParentGroups这一栏应该是没有ParentGroup时为空,只显示SubGroup的ParentGroups.有多个ParentGroup时用";"隔开.
下面是相关代码,请问懂AD的大侠们该如何修改呢?

C# code

private DataTable ADGroup;

private void InitialADGroupTableSchema()
        {
            ADGroup = new DataTable("ADGSSGroup");

            ADGroup.Columns.Add("ID", typeof(string));
            ADGroup.Columns.Add("Name", typeof(string));
            ADGroup.Columns.Add("Description", typeof(string));
            ADGroup.Columns.Add("GroupType", typeof(int));
            ADGroup.Columns.Add("Owner", typeof(string));
            ADGroup.Columns.Add("Coowner", typeof(string));
            ADGroup.Columns.Add("CreatedDate", typeof(DateTime));
            ADGroup.Columns.Add("ChangedDate", typeof(DateTime));
            ADGroup.Columns.Add("IsGSSGroup", typeof(bool));
            ADGroup.Columns.Add("AutoAcceptOUChange", typeof(bool));
            ADGroup.Columns.Add("CompanyCode", typeof(string));
            ADGroup.Columns.Add("AutoExpiredDate", typeof(DateTime));
            ADGroup.Columns.Add("ParentGroups", typeof(string));
        }

private DataRow GetDataRowFromADEntry(DirectoryEntry ADEntry)
        {
            string objName = ADEntry.Name.Remove(0, 3);
            //
            DataRow dr = ADGroup.NewRow();
            dr["ID"] = objName;
            dr["name"] = objName;
            dr["Description"] = Equals(ADEntry.Properties["Description"].Value, null)
                                    ? string.Empty
                                    : ADEntry.Properties["Description"].Value.ToString();
            dr["Owner"] = string.Empty;
            dr["Coowner"] = string.Empty;
            dr["IsGSSGroup"] = true;
            dr["AutoAcceptOUChange"] = true;
            dr["GroupType"] = int.Parse(ADEntry.Properties["groupType"].Value.ToString());
            dr["CreatedDate"] = DateTime.Parse(ADEntry.Properties["whenCreated"].Value.ToString());
            dr["ChangedDate"] = DateTime.Parse(ADEntry.Properties["whenChanged"].Value.ToString());
            dr["CompanyCode"] = GetCompanyCode(ADEntry.Path);
            dr["AutoExpiredDate"] = DateTime.Now.AddMonths(12);
            DirectoryEntry deParent = ADEntry.Parent;
            dr["ParentGroups"] = deParent.SchemaClassName.ToLower().Equals("group") ? deParent.Name.Remove(0, 3) : string.Empty;

            return dr;
        }

private void FetchNestedGroups(string groupDN)
        {
            using (var groupDE = new DirectoryEntry(ADServer + RootPath, UserName, Password, AuthenticationTypes.Secure))
            {
                var ds = new DirectorySearcher
                             {
                                 SearchRoot = groupDE,
                                 Filter =
                                     String.Format("(&(memberOf={0})(objectCategory=group)(objectClass=group))", groupDN),
                                 ReferralChasing = ReferralChasingOption.All
                             };
 
                ds.PropertiesToLoad.Add("GRPOBJID");
                ds.PropertiesToLoad.Add("name");
                ds.PropertiesToLoad.Add("description");
                ds.PropertiesToLoad.Add("groupType");
                ds.PropertiesToLoad.Add("whenCreated");
                ds.PropertiesToLoad.Add("whenC