这与this post有关,我试图做同样的事情。因为我无法让它发挥作用,我尝试了不同的方法。我的目标是让所有用户进入一个集合并循环遍历集合加载参数,以获得与前一个帖子类似的存储过程。
这次我尝试了AccountManagement Class,我可以获得用户的所有属性,但这次我不知道获取组的组或描述的语法。我假设我需要简单地加载某些类型的集合,这些集合来自UserPrincipal,例如up.GetGroups()
,并通过它进行枚举,但我正在努力学习语法。使用该代码,我还需要能够访问描述的内容。
PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain", "[my path]");
UserPrincipal ADUser = new UserPrincipal(AD);
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = ADUser;
PrincipalSearchResult<Principal> result = ps.FindAll();
foreach (Principal p in result)
using (UserPrincipal up = (UserPrincipal)p)
{
if (up.AccountExpirationDate.HasValue)
Debug.WriteLine(up.AccountExpirationDate.ToString());
if (up.AccountLockoutTime.HasValue)
Debug.WriteLine(up.BadLogonCount.ToString());
if (up.DisplayName != null)
Debug.WriteLine(up.DisplayName.ToString());
if (up.DistinguishedName != null)
Debug.WriteLine(up.DistinguishedName.ToString());
if (up.EmailAddress != null)
Debug.WriteLine(up.EmailAddress.ToString());
if (up.EmployeeId != null)
Debug.WriteLine(up.EmployeeId.ToString());
if (up.Enabled.HasValue)
if (up.Enabled == true)
Debug.WriteLine("User is active");
else
Debug.WriteLine("User is deactivated");
if (up.GivenName != null)
Debug.WriteLine(up.GivenName.ToString());
if (up.LastBadPasswordAttempt.HasValue)
Debug.WriteLine(up.LastBadPasswordAttempt.ToString());
if (up.LastLogon.HasValue)
Debug.WriteLine(up.LastLogon.ToString());
if (up.LastPasswordSet.HasValue)
Debug.WriteLine(up.LastPasswordSet.ToString());
if (up.MiddleName != null)
Debug.WriteLine(up.MiddleName.ToString());
if (up.Name != null)
Debug.WriteLine(up.Name.ToString());
if (up.PasswordNeverExpires != null)
if (up.PasswordNeverExpires == true)
Debug.Print("User Password Never Expires");
else
Debug.WriteLine("User Password Expires");
if (up.SamAccountName != null)
Debug.WriteLine(up.SamAccountName.ToString());
if (up.Sid != null)
Debug.WriteLine(up.Sid.ToString());
if (up.Surname != null)
Debug.WriteLine(up.Surname.ToString());
if (up.UserPrincipalName != null)
Debug.WriteLine(up.UserPrincipalName.ToString());
if (up.VoiceTelephoneNumber != null)
Debug.WriteLine(up.VoiceTelephoneNumber.ToString());
}
我尝试使用GroupPrincipal
,但在编写时,我可以看到“说明”字段,但我看不到任何其他内容。我尝试使用以下代码:
//PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain]", "[my path]");
GroupPrincipal theGroup = new GroupPrincipal(AD);
PrincipalSearcher gps = new PrincipalSearcher(theGroup);
foreach (var found in gps.FindAll())
{
if (found.Description != null)
{
Debug.WriteLine(found.Description.ToString());
}
if (found.DisplayName != null)
{
Debug.WriteLine(found.DisplayName.ToString());
}
}
此代码可以很好地获取组的描述,但我看不到任何其他内容,因为所有其他字段都为空。
非常感谢任何和所有帮助。
答案 0 :(得分:0)
我相信我在这篇文章中解决了这个问题。下面是从Active Directory填充对象并进入嵌套循环的代码。第一个循环遍历每个用户,第二个循环遍历用户所属的每个组,然后移动到下一个用户。在每个循环中,我将参数传递给存储过程,这些存储过程执行insert语句,其值为两个sepearate表,一个用于用户属性,第二个用于组属性。第一个存储过程传回每行的标识,以用作第二个表中的FK。
对于某些参数是单词“NotFoundYet”,因为UserPrincipal没有像DirectorySearcher
那样可用的字段在我第一次尝试写这个时。这也回答了我Active Directory description field values not showing up的问题,也是我发布的。
从技术上讲,我仍然需要代码才能找到那些缺少的属性,但我可能会帮助别人,而我仍在尝试找到我没有的东西。我稍后会更新。特别感谢Jeff Ronay对此的帮助。
PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domanin]", "[my path]");
UserPrincipal ADUser = new UserPrincipal(AD);
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = ADUser;
PrincipalSearchResult<Principal> result = ps.FindAll();
foreach (UserPrincipal CurrentUser in result)
{
PrincipalSearchResult<Principal> userGroups = CurrentUser.GetGroups();
using (SqlConnection dataConnection = new SqlConnection("[my sql connection]"))
{
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", "NotFoundYet");
if (CurrentUser.GivenName != null)
{
dataCommand.Parameters.AddWithValue("@givenName", CurrentUser.GivenName.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@givenName", "Empty");
}
dataCommand.Parameters.AddWithValue("@initials", "NotFoundYet");
if (CurrentUser.Surname != null)
{
dataCommand.Parameters.AddWithValue("@sn", CurrentUser.Surname.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@sn", "Empty");
}
if (CurrentUser.EmailAddress != null)
{
dataCommand.Parameters.AddWithValue("@mail", CurrentUser.EmailAddress.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@mail", "Empty");
}
if (CurrentUser.Name != null)
{
dataCommand.Parameters.AddWithValue("@Name", CurrentUser.Name.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@Name", "Empty");
}
if (CurrentUser.MiddleName != null)
{
dataCommand.Parameters.AddWithValue("@middleName", CurrentUser.MiddleName.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@middleName", "N/A");
}
dataCommand.Parameters.AddWithValue("@title", "NotFoundYet");
if (CurrentUser.EmployeeId != null)
{
dataCommand.Parameters.AddWithValue("@employeeID", CurrentUser.EmployeeId.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@employeeID", "Empty");
}
dataCommand.Parameters.AddWithValue("@employeeNumber", "NotFoundYet");
if (CurrentUser.Sid != null)
{
dataCommand.Parameters.AddWithValue("@objectSid", CurrentUser.Sid.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@objectSid", "Empty");
}
dataCommand.Parameters.AddWithValue("@userAccountControl", "NotFoundYet" );
dataCommand.Parameters.AddWithValue("@whenCreated", "NotFoundYet");
if (CurrentUser.DistinguishedName != null)
{
dataCommand.Parameters.AddWithValue("@distinguishedName", CurrentUser.DistinguishedName.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@distinguishedName", "Empty");
}
dataCommand.Parameters.AddWithValue("@badPasswordTime", "NotFoundYet"); //Issues!!
if (CurrentUser.BadLogonCount != null)
{
dataCommand.Parameters.AddWithValue("@badPwdCount", CurrentUser.BadLogonCount.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@badPwdCount", "Empty");
}
dataCommand.Parameters.AddWithValue("@memberof", "Empty");
if (CurrentUser.SamAccountName != null)
{
dataCommand.Parameters.AddWithValue("@samaccountname", CurrentUser.SamAccountName.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@samaccountname", "Empty");
}
if (CurrentUser.Description != null)
{
dataCommand.Parameters.AddWithValue("@Description", CurrentUser.Description.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@Description", "Empty");
}
dataCommand.Parameters.AddWithValue("@maxPwdAge", "NotFoundYet"); //Issues!!
if (CurrentUser.LastPasswordSet != null)
{
dataCommand.Parameters.AddWithValue("@pwdLastSet", CurrentUser.LastPasswordSet.ToString()); //Issues!!
}
else
{
dataCommand.Parameters.AddWithValue("@pwdLastSet", "Empty"); //Issues!!
}
if (CurrentUser.AccountLockoutTime != null)
{
dataCommand.Parameters.AddWithValue("@LockOutTime", CurrentUser.AccountLockoutTime.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@LockOutTime", "Empty"); //Issues!!
}
if (CurrentUser.Enabled == false) //Issues!!
{
dataCommand.Parameters.AddWithValue("@Acctdisabled", '0');
}
else
{
dataCommand.Parameters.AddWithValue("@Acctdisabled", '1');
}
if (CurrentUser.DisplayName != null)
{
dataCommand.Parameters.AddWithValue("@displayname", CurrentUser.DisplayName.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@displayname", "Empty");
}
dataCommand.Parameters.AddWithValue("@twofactor", "NotFoundYet"); //Calculated from another field
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;
} //End of Datacommand
} //End of Sql Connection
using (SqlConnection dataConnection = new SqlConnection("[my sql connection]"))
{
using (SqlCommand dataCommand = dataConnection.CreateCommand())
{
dataConnection.Open();
foreach (Principal group in userGroups)
{
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 (group.Description != null)
{
Debug.WriteLine(group.Description.ToString());
dataCommand.Parameters.AddWithValue("@GroupDescription", group.Description.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@GroupDescription", "Empty");
}
if (group.Name != null)
{
Debug.WriteLine(group.Name.ToString());
dataCommand.Parameters.AddWithValue("@memberOf", group.Name.ToString());
}
else
{
dataCommand.Parameters.AddWithValue("@memberOf", "Empty");
}
dataCommand.ExecuteScalar();
InnerCounter++;
} //End of 'For Each Principle'
}//End of DataCommand
} //End of Data Connection
} //End of 'For Each User' Loop