我正在努力从SSIS中的C#(3.5框架)中的脚本任务中获取Active Directory中的值。实现这一目标的代码似乎工作正常。我使用一个嵌套循环,外部循环拉取非多值的值,并将它们插入到具有存储过程的表中,然后将该行的PK传递给第二个循环中的FK用作第二个循环将所有用户所属的组插入第二个表的存储过程。
我的问题是,我不能从我认为应该是Description
字段的任何值中得到任何值,该字段应该包含每个组的描述。如果我单步执行并检查描述对象的计数,则显示0.使用相同的代码检查Memberof
的计数我得到用户所属的组的数量。我不理解的另一部分是我从外部循环中的Description
字段获取值,但它更接近用户记录的注释,而不是单个组的描述。好像我的Description
不是多值的。但是其他人可以通过AD门户确认Description
字段具有描述每个组的值的值。我有点难过。请参阅下面的代码。
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
答案 0 :(得分:1)
LDAP搜索查询有点像SQL查询,我知道它没有明确记录,但您最好通过搜索声明要重新搜索的属性。你能尝试添加
吗?DirectorySearcher Dsearch = new DirectorySearcher(entry);
...
Dsearch.PropertiesToLoad.Add("description");
...
Dsearch.Filter = "(&(objectClass=User))";
在我的代码中,我真的添加了我需要的所有属性。我同意它在大多数情况下没有这种情况,但大部分时间都是如此。