AccessActiveDirectory






3.15/5 (10投票s)
2005年3月9日
4分钟阅读

50101

1288
一篇展示“AccessActiveDirectory”实用程序如何操作指定域中的 Active Directory 成员的文章。
引言
本文档重点介绍了 AccessActiveDirectory 实用程序的特性,该实用程序可以对 Active Directory (AD) 执行一组列出的操作。
该实用程序为您处理了几乎所有事情
- 将成员添加到 AD。
- 从 AD 中移除成员。
- 在 AD 中搜索成员。
- 在 AD 中创建成员。
- 从 AD 中删除成员。
- 从 AD 获取成员的属性。
- 更新 AD 中成员的属性。
- 检查给定对象是否存在于 AD 中。
AccessActiveDirectory - 方法定义
构造函数
该组件有三个重载的构造函数。第一个构造函数用于定义 TargetType
(枚举器 - 在源文件中定义)、目标名称以及将添加到/移除到指定目标的对象源。使用此构造函数的任何客户端都应调用 SetAdsPath()
方法来设置目标对象路径。
签名
public AccessActiveDirectory(TargetType objtargetType,
string strTargetName, string strDomainPath)
objtargetType
-TargetType
定义目标的类型。它是一个枚举器,可以取以下三种值中的任何一种(用户、计算机、组)。strTargetName
- 目标对象的名称。它可以是组、用户或计算机名称。strDomainPath
- 源对象域路径。
第二个构造函数用于定义 TargetType
(枚举器 - 在源文件中定义)、目标名称、将添加到/移除到指定目标的对象源以及 PDCEmulator(如果我们有多个主域服务器)。使用此构造函数的任何客户端都应调用 SetAdsPath()
方法来设置目标对象路径。
签名
public AccessActiveDirectory(TargetType objtargetType,
string strTargetName,
string strDomainPath,
string strPDCEmulator)
objtargetType
-TargetType
定义目标的类型。它是一个枚举器,可以取以下三种值中的任何一种(用户、计算机、组)。strTargetName
- 目标对象的名称。它可以是组、用户或计算机名称。strDomainPath
- 源对象域路径。strPDCEmulator
- PDC 模拟器的名称。
第三个也是最后一个构造函数用于定义 TargetPath
以及将添加到/移除到指定目标的对象源。
签名
public AccessActiveDirectory(string strTargetPath, string strDomainPath)
strTargetPath
- 目标对象 AD 域路径。strDomainPath
- 源对象域路径。
将成员添加到组
此函数将成员(strDomainPath
)添加到构造函数中指定的目標組。成员可以是用户、计算机或组,它们可以添加到另一个组中。
签名
public void AddMembersToGroup(string strMemberPath)
strMemberPath
- 要添加的成员对象的 AD 路径。
// Get the group object
direntGroup = new DirectoryEntry(mstrTargetPath);
// Add the member to the group
direntGroup.Invoke("Add", new Object[] { strMemberPath });
从组中移除成员
此函数从构造函数中指定的目標組中移除现有的成员 strDomainPath
。成员可以是用户、计算机或组,它们可以从另一个组中移除。
签名
public void RemoveMembersFromGroup(string strMemberPath)
strMemberPath
- 要移除的成员对象的 AD 路径。
// Get the group object
direntGroup = new DirectoryEntry(mstrTargetPath);
// Add the member to the group
direntGroup.Invoke("Remove", new Object[] { strMemberPath });
成员计数检查
此函数检查目标对象的成员计数是否超过 5000。这是为了确保组不会因为成员过多而导致问题。相反,可以在主组下创建一个子组,并将成员添加到该子组中。这样,未来维护就不会受到威胁。您可以根据需要自定义计数。
签名
public bool IsMemberCountExceeds()
// Get the member count
intMemberCount = direntTarget.Properties["Member"].Count;
// If the member count greater than 5000
if(intMemberCount > 5000)
{
return true;
}
else
{
return false;
}
获取成员属性
此函数获取目标对象的属性。
签名
public string GetPropertyOfMember(string strProperty)
// start searching from local domain
dirsrcTarget.SearchRoot = new DirectoryEntry(mstrTargetPath);
// Get the filter string based on TargetType/TargetName
dirsrcTarget.Filter = GetFilterString();
// start searching for the first object
objSearchResult = dirsrcTarget.FindOne();
// If thers is no records
if(objSearchResult == null)
{
// throw no Record
throw new VinodException("INF-UTY-001");
}
// Get the directory entries of the selected one
direntTarget = objSearchResult.GetDirectoryEntry();
// return the directoryentry object
objMemberColln = direntTarget.Properties[strProperty];
设置 AD 路径
此函数使用构造函数中提供的目标名称/类型来设置目标的 AD 路径。如上所述,如果您使用的是前两个构造函数,则必须调用此方法来设置目标 AD 的路径。它在 Active Directory 中搜索目标对象,以获取目标对象的 AD 路径并将其设置为成员变量。
签名
public void SetAdsPath()
获取 CN 路径
此函数获取给定 AdsPath 的第一个 CN,通常是完全限定的域名。
签名
public string GetCNOfAdsPath(string strAdsPath)
获取 MemberOf 目标
此函数从目标对象获取 memberOf
属性值。
签名
public PropertyValueCollection GetMembersOfGroup()
获取目标成员
此函数从目标对象获取 members
属性值。
签名
public PropertyValueCollection GetGroupMembers()
创建组
此函数使用提供的组名在给定的容器路径下创建一个新组。
签名
public void CreateGroup(string strGroupContainerPath, string strGroupDesc)
对象存在
这是一个简单的函数,用于检查给定对象是否存在。
签名
public bool IsObjectExist(string strAdsPath)
更新对象的属性
此函数更新指定对象(strAdsPath
)的属性值集合。它会检查对象是否存在于 AD 中,如果存在,则会将作为 HashTable
提供的所有属性更新到 AD 中。
签名
public void UpdatePropertiesForADObject(string strAdsPath,
Hashtable htblProperties)
// start searching from local domain
direntTarget = new DirectoryEntry(strAdsPath);
// Get all the enumerators
objIDictEnum = htblProperties.GetEnumerator();
//Get properties of all the hash table entries
while(objIDictEnum.MoveNext())
{
if(objIDictEnum.Value.GetType() == typeof(string))
{
// Include the property
direntTarget.Invoke("Put", objIDictEnum.Key.ToString(),
objIDictEnum.Value.ToString());
}
else if(objIDictEnum.Value.GetType() == typeof(Int32))
{
// Include the property
direntTarget.Invoke("Put", objIDictEnum.Key.ToString(),
Convert.ToInt16(objIDictEnum.Value));
}
}
// set info
direntTarget.Invoke("SetInfo");
// Commit the changes
direntTarget.CommitChanges();
摘要
此实用程序对于那些大量使用 Active Directory 的人来说应该非常有用。每当我搜索网络时,只能零散地找到一些东西……所以……我决定提供一个至少能完成 AD 上一些基本操作的实用程序。我还没有包含创建用户,等有时间我会加上。在此之前,请享用这个!