Active Directory中的描述字段值显示不出来字段、不出来、Active、Directory

2023-09-08 13:19:53 作者:爱情是一份甜蜜の忧伤

我的工作从下在SSIS#(3.5框架)的脚本任务获得从Active Directory值。在code来完成,这似乎是工作的罚款。予使用嵌套循环与外环拉动不在多值值,并把它们插入到一个表的存储过程,然后使该行的背面的PK被用作FK在第二循环中的第二存储过程,将所有该用户所属的第二个表。组

我的问题是我无法从我所相信的应该是说明字段应该持有每个组的描述得到任何价值。如果我一步通过它,并检查说明对象的数量,它显示为0。用同样的code检查的memberOf 的计数我得到群体的数量用户所属。我不明白,另一部分则是我从描述值字段中外环,但它更接近于用户记录的意见,没有说明各组。它好像我的说明不是多值。但别人可以通过AD门户的说明字段描述了每个组值确认。我有点难倒。见下面code。

 的DirectoryEntry条目=新的DirectoryEntry(LDAP:[我的地址]);
DirectorySearcher从Dsearch =新DirectorySearcher从(输入);
Dsearch.Filter =(及(objectClass的=用户));

的foreach(信息搜索结果sResultSet在Dsearch.FindAll())
{
    (code继续从原帖)

    使用(SqlConnection的dataConnection =新的SqlConnection([的MySqlConnection]))
    {
        使用(SqlCommand的dataCommand = dataConnection.CreateCommand())
        {
           dataCommand.CommandText =ActiveDirectory.InsertParentRecords;
           dataCommand.CommandType = CommandType.StoredProcedure;

           dataCommand.Parameters.AddWithValue(@ PackageLogId,Dts.Variables [PackageLogId] Value.ToString());
           dataCommand.Parameters.AddWithValue(@ CN的getProperty(sResultSet,CN));
           dataCommand.Parameters.AddWithValue(@给定名称,的getProperty(sResultSet,给定名称));
           dataCommand.Parameters.AddWithValue(@缩写的getProperty(sResultSet,英文缩写));
           dataCommand.Parameters.AddWithValue(@ SN的getProperty(sResultSet,SN));

           dataCommand.Parameters.AddWithValue(@邮件的getProperty(sResultSet,邮件));
           dataCommand.Parameters.AddWithValue(@名称的getProperty(sResultSet,姓名));
           dataCommand.Parameters.AddWithValue(@ middleName,的getProperty(sResultSetmiddleName));
           dataCommand.Parameters.AddWithValue(@题,的getProperty(sResultSet,标题));
           dataCommand.Parameters.AddWithValue(@雇员的getProperty(sResultSet,雇员));

           dataCommand.Parameters.AddWithValue(@ employeeNumber,的getProperty(sResultSetemployeeNumber));
           dataCommand.Parameters.AddWithValue(@的objectSID,ConvertSidToString((字节[])sResultSet.Properties [的objectSID] [0]));
           dataCommand.Parameters.AddWithValue(@ userAccountControl的,tempuserAccountControl);
           dataCommand.Parameters.AddWithValue(@ whenCreated,的getProperty(sResultSetwhenCreated));
           dataCommand.Parameters.AddWithValue(@的distinguishedName的getProperty(sResultSet的distinguishedName));

           dataCommand.Parameters.AddWithValue(@ badPasswordTime,Convert.ToString(badPasswordTime)); //问题!
           dataCommand.Parameters.AddWithValue(@ badPwdCount,的getProperty(sResultSetbadPwdCount));
           dataCommand.Parameters.AddWithValue(@成员,的getProperty(sResultSet,成员));
           dataCommand.Parameters.AddWithValue(@ SAM帐户,的getProperty(sResultSet,SAM帐户));
           dataCommand.Parameters.AddWithValue(@说明的getProperty(sResultSet,说明));

           dataCommand.Parameters.AddWithValue(@ maxPwdAge,的getProperty(sResultSetmaxPwdAge)); //问题!
           dataCommand.Parameters.AddWithValue(@的pwdLastSet的pwdLastSet); //问题!
           dataCommand.Parameters.AddWithValue(@ LockOutTime,Convert.ToString(LockOutTime)); //问题!

           如果(禁用== FALSE)//问题!
              dataCommand.Parameters.AddWithValue(@ Acctdisabled,0);
           其他
              dataCommand.Parameters.AddWithValue(@ Acctdisabled,1);

           dataCommand.Parameters.AddWithValue(@显示名称的getProperty(sResultSet,显示名称));

           dataCommand.Parameters.AddWithValue(@ twofactor,twofactor);
           dataCommand.Parameters.AddWithValue(@滑雪precord,滑雪precord);

           dataCommand.Parameters.Add(@ DetailID,SqlDbType.Int);
           dataCommand.Parameters [@ DetailID]方向= ParameterDirection.Output。

           dataConnection.Open();
           dataCommand.ExecuteScalar();
           dataConnection.Close();

           反++;
           DetailID =(INT)dataCommand.Parameters [@ DetailID]值。
       }
    }

    使用(SqlConnection的dataConnection =新的SqlConnection [的MySqlConnection]))
    {
       使用(SqlCommand的dataCommand = dataConnection.CreateCommand())
       {
           dataConnection.Open();

           INT groupCount = sResultSet.Properties [成员]计数。
           。INT DescriptionCount = sResultSet.Properties [说明]计算;

           对于(INT计数器= 0;反< groupCount;反++)
           {
               dataCommand.CommandText =ActiveDirectory.InsertMemberOf;
               dataCommand.CommandType = CommandType.StoredProcedure;

               dataCommand.Parameters.Clear();
               dataCommand.Parameters.AddWithValue(@ PackageLogId,Dts.Variables [PackageLogId] Value.ToString());
               dataCommand.Parameters.AddWithValue(@ DetailID,DetailID);

               如果(sResultSet.Properties.Contains(描述))
               {
                  dataCommand.Parameters.AddWithValue(@集团,sResultSet.Properties [说明] [计数器]的ToString());
               }
               其他
               {
                  dataCommand.Parameters.AddWithValue(@集团,N / A);
               }

               dataCommand.Parameters.AddWithValue(@成员,sResultSet.Properties [成员] [计数器]);

               dataCommand.ExecuteScalar();
               InnerCounter ++;
            }
         } // DataCommand结束
     } //数据连接结束


     的Debug.WriteLine(的getProperty(sResultSet,显示名)++计数器+,+ InnerCounter +,+的getProperty(sResultSetuserAccountControl的));
     InnerCounter = 0;
  } //结束的每个回路
 

解决方案

一个LDAP搜索查询是有点像一个SQL查询,我知道它没有明确记载,但你最好声明要由被retrived属性搜索。你可以尝试添加

  DirectorySearcher从Dsearch =新DirectorySearcher从(输入);
...
Dsearch.PropertiesToLoad.Add(说明);
...
Dsearch.Filter =(及(objectClass的=用户));
 
Active Directory与域服务,介绍,安装

在我的code我真的添加所有我需要的属性。我同意它的工作原理没有,大部分的时间,只是大部分时间。

I'm working on getting values from Active Directory from a script task in C# (3.5 framework) in SSIS. The code to accomplish this seems to be working fine. I use a nested loop with the outer loop pulling values that aren't multi-valued and inserting them to a table with a stored procedure, then passing back the PK of that row to be used as a FK in the second loop to a second stored procedure that inserts all the groups that user belongs to in a second table.

My problem is I can't get any values from what I believe should be the Description field that should hold the description of each individual group. If I step through it and check the count of description object, it shows 0. Using the same code to check the count of Memberof I get the number of groups that user belongs to. The other part that I don't understand is I get values from the Description field in the outer loop, but its closer to that of comments of the user record, not descriptions of the individual groups. Its as if my Description is not multivalued. But someone else can confirm through the AD portal that the Description field has values that describe what each group is. I'm kind of stumped. See code below.

DirectoryEntry entry = new DirectoryEntry("LDAP:[my address]");
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(&(objectClass=User))"; 

foreach (SearchResult sResultSet in Dsearch.FindAll())
{
    (code continues from original post)

    using (SqlConnection dataConnection = new SqlConnection([mysqlconnection]))
    {
        using (SqlCommand dataCommand = dataConnection.CreateCommand())
        {
           dataCommand.CommandText = "ActiveDirectory.InsertParentRecords";
           dataCommand.CommandType = CommandType.StoredProcedure;

           dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
           dataCommand.Parameters.AddWithValue("@cn", GetProperty(sResultSet, "cn"));
           dataCommand.Parameters.AddWithValue("@givenName", GetProperty(sResultSet, "givenName"));
           dataCommand.Parameters.AddWithValue("@initials", GetProperty(sResultSet, "initials"));
           dataCommand.Parameters.AddWithValue("@sn", GetProperty(sResultSet, "sn"));

           dataCommand.Parameters.AddWithValue("@mail", GetProperty(sResultSet, "mail"));
           dataCommand.Parameters.AddWithValue("@Name", GetProperty(sResultSet, "Name"));
           dataCommand.Parameters.AddWithValue("@middleName", GetProperty(sResultSet, "middleName"));
           dataCommand.Parameters.AddWithValue("@title", GetProperty(sResultSet, "title"));
           dataCommand.Parameters.AddWithValue("@employeeID", GetProperty(sResultSet, "employeeID"));

           dataCommand.Parameters.AddWithValue("@employeeNumber", GetProperty(sResultSet, "employeeNumber"));
           dataCommand.Parameters.AddWithValue("@objectSid", ConvertSidToString((byte[])sResultSet.Properties["objectSid"][0]));
           dataCommand.Parameters.AddWithValue("@userAccountControl", tempuserAccountControl);
           dataCommand.Parameters.AddWithValue("@whenCreated", GetProperty(sResultSet, "whenCreated"));
           dataCommand.Parameters.AddWithValue("@distinguishedName", GetProperty(sResultSet, "distinguishedName"));

           dataCommand.Parameters.AddWithValue("@badPasswordTime", Convert.ToString(badPasswordTime));  //Issues!!
           dataCommand.Parameters.AddWithValue("@badPwdCount", GetProperty(sResultSet, "badPwdCount"));
           dataCommand.Parameters.AddWithValue("@memberof", GetProperty(sResultSet, "memberof"));
           dataCommand.Parameters.AddWithValue("@samaccountname", GetProperty(sResultSet, "samaccountname"));
           dataCommand.Parameters.AddWithValue("@Description", GetProperty(sResultSet, "Description"));

           dataCommand.Parameters.AddWithValue("@maxPwdAge", GetProperty(sResultSet, "maxPwdAge"));   //Issues!!                               
           dataCommand.Parameters.AddWithValue("@pwdLastSet", pwdLastSet);   //Issues!!
           dataCommand.Parameters.AddWithValue("@LockOutTime", Convert.ToString(LockOutTime));     //Issues!!

           if (disabled == false)  //Issues!!
              dataCommand.Parameters.AddWithValue("@Acctdisabled", '0');
           else
              dataCommand.Parameters.AddWithValue("@Acctdisabled", '1');

           dataCommand.Parameters.AddWithValue("@displayname", GetProperty(sResultSet, "displayname"));

           dataCommand.Parameters.AddWithValue("@twofactor", twofactor);
           dataCommand.Parameters.AddWithValue("@skiprecord", skiprecord);

           dataCommand.Parameters.Add("@DetailID", SqlDbType.Int);
           dataCommand.Parameters["@DetailID"].Direction = ParameterDirection.Output;

           dataConnection.Open();
           dataCommand.ExecuteScalar();
           dataConnection.Close();

           Counter++;
           DetailID = (int)dataCommand.Parameters["@DetailID"].Value;
       }
    }

    using (SqlConnection dataConnection = new SqlConnection[mysqlconnection]))
    {
       using (SqlCommand dataCommand = dataConnection.CreateCommand())
       {
           dataConnection.Open();

           int groupCount = sResultSet.Properties["memberOf"].Count;
           int DescriptionCount = sResultSet.Properties["Description"].Count;

           for (int counter = 0; counter < groupCount; counter++)
           {
               dataCommand.CommandText = "ActiveDirectory.InsertMemberOf";
               dataCommand.CommandType = CommandType.StoredProcedure;

               dataCommand.Parameters.Clear();
               dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString());
               dataCommand.Parameters.AddWithValue("@DetailID", DetailID);

               if (sResultSet.Properties.Contains("Description"))
               {
                  dataCommand.Parameters.AddWithValue("@Group", sResultSet.Properties["Description"][counter].ToString());
               }
               else
               {
                  dataCommand.Parameters.AddWithValue("@Group", "n/a");
               }                               

               dataCommand.Parameters.AddWithValue("@memberOf", sResultSet.Properties["memberOf"][counter]);   

               dataCommand.ExecuteScalar();
               InnerCounter++;
            }
         }  //End of DataCommand
     }  //End of Data Connection 


     Debug.WriteLine(GetProperty(sResultSet, "displayname") + "  " + Counter + ",  " + InnerCounter + ",  " + GetProperty(sResultSet, "userAccountControl"));
     InnerCounter = 0;                        
  } //End of For Each Loop

解决方案

A LDAP Search query is a bit like a SQL query, I know It's not clearly documented but you'd better declare the properties you want to be retrived by the search. Can you try to add

DirectorySearcher Dsearch = new DirectorySearcher(entry);
...
Dsearch.PropertiesToLoad.Add("description");
...
Dsearch.Filter = "(&(objectClass=User))"; 

In my code I really add all the properties I need. I agree it works without that most of the time, but just most of the time.