📄 adservices.cs
字号:
#endregion
#region[电脑清单]
DataView IQueryList.GetComputerList(string ADSPath)
{
strFilter = "(objectClass=computer)";
strPropertiesToLoad = "name,managedBy,ADsPath";
try
{
//***********执行方法************
//得到查询结果,并赋给mySearcher
GetOUList(ADSPath,strFilter,strPropertiesToLoad,SearchScope.Subtree);
//通过mySearcher的值创建一张表
NewTable(mySearcher);
//*******************************
myView = myTable.DefaultView;
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
finally
{
myEntry.Close();
myEntry.Dispose();
mySearcher.Dispose();
}
return myView;
}
#endregion
#region[组的成员]
/// <summary>
/// 得到组的成员
/// </summary>
/// <param name="strDomain">域名</param>
/// <param name="strGroup">组名</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetGroupMembersArr(string strDomain, string strGroup)
{
StringCollection groupMemebers = new StringCollection();
string strAdsPath = "LDAP://DC=" + strDomain + ",DC=esquel,DC=com";
strFilter = "(CN=" + strGroup + ")";
strPropertiesToLoad = "member";
try
{
//***********执行方法************
//得到查询结果,并赋给mySearcher
GetOUList(strAdsPath,strFilter,strPropertiesToLoad,SearchScope.Subtree);
//*******************************
SearchResultCollection coll = mySearcher.FindAll();
foreach (SearchResult rs in coll)
{
ResultPropertyCollection resultPropColl = rs.Properties;
foreach(object memberColl in resultPropColl[strPropertiesToLoad])
{
object obVal = GetNTAccount(memberColl.ToString());
if (null != obVal)
groupMemebers.Add(obVal.ToString().ToLower());
}
}
}
catch (Exception err)
{
strErrDescription = err.ToString();
}
finally
{
myEntry.Close();
myEntry.Dispose();
mySearcher.Dispose();
}
return groupMemebers;
}
/// <summary>
/// 得到组的成员
/// </summary>
/// <param name="strGroup">组名</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetGroupMembersArr(string strGroup)
{
IQueryList objGetGroupMembersArr = (IQueryList)new ADServices();
return objGetGroupMembersArr.GetGroupMembersArr(CONST_DOMAIN,strGroup);
}
#endregion
#region[组的清单]
/// <summary>
/// 得到组的清单
/// </summary>
/// <param name="ADSPath">LDAP://地址</param>
/// <returns>DataView</returns>
DataView IQueryList.GetGroupList(string ADSPath)
{
strFilter = "(objectClass=Group)";
strPropertiesToLoad = "name,ADsPath";
try
{
//***********执行方法************
//得到查询结果,并赋给mySearcher
GetOUList(ADSPath,strFilter,strPropertiesToLoad,SearchScope.Subtree);
//通过mySearcher的值创建一张表
NewTable(mySearcher);
//*******************************
myView = myTable.DefaultView;
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
finally
{
myEntry.Close();
myEntry.Dispose();
mySearcher.Dispose();
}
return myView;
}
/// <summary>
/// 组的清单
/// </summary>
/// <param name="ADSPath">LDAP://地址</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetGroupArr(string ADSPath)
{
strFilter = "(objectClass=Group)";
strPropertiesToLoad = "name";
strCollection = new StringCollection();
try
{
//***********执行方法************
//得到查询结果,并赋给mySearcher
GetOUList(ADSPath,strFilter,strPropertiesToLoad,SearchScope.Subtree);
//*******************************
foreach(SearchResult res in mySearcher.FindAll())
{
strCollection.Add(res.Properties[strPropertiesToLoad][0].ToString());
}
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
finally
{
myEntry.Close();
myEntry.Dispose();
mySearcher.Dispose();
}
return strCollection;
}
#endregion
#region[对象的地址]
/// <summary>
/// 得到对象的地址[LDAP://]
/// </summary>
/// <param name="ADsPath">查询的地址[LDAP://]</param>
/// <param name="adName">查询对象的名字</param>
/// <param name="enumType">对象类别</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjPath(string ADsPath,string adName,QueryType enumType)
{
strCollection = new StringCollection();
myEntry = new DirectoryEntry(ADsPath);
mySearcher = new DirectorySearcher(myEntry);
switch(enumType)
{
//用户名
case QueryType.UserName:
string strUserPath = UserPath(myEntry,adName);
if (strUserPath == "")
strErrDescription = "UserName:"+adName+" - no fond.";
else
strCollection.Add(strUserPath);
return strCollection;
//组名
case QueryType.GroupName:
string strGroupPath = GroupPath(myEntry,adName);
if (strGroupPath == "")
strErrDescription = "GroupName:"+adName+" - no fond.";
else
strCollection.Add(strGroupPath);
return strCollection;
//计算机名
case QueryType.ComputerName:
mySearcher.Filter = ("(&(objectClass=computer)(name="+adName+"))");
foreach(SearchResult res in mySearcher.FindAll())
{
strCollection.Add(res.Properties["AdsPath"][0].ToString());
}
if (strCollection.Count == 0)
{
strErrDescription = "ComputerName:"+adName+" - no fond.";
return strCollection;
}
else
return strCollection;
//单元名
case QueryType.OUName:
mySearcher.Filter = ("(&(objectCategory=CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=esquel,DC=com)(name="+adName+"))");
foreach(SearchResult res in mySearcher.FindAll())
{
strCollection.Add(res.Properties["AdsPath"][0].ToString());
}
if (strCollection.Count == 0)
{
strErrDescription = "OU Name:"+adName+" - no fond.";
return strCollection;
}
else
return strCollection;
default:
return strCollection;
}
}
/// <summary>
/// 得到对象的地址[LDAP://]
/// </summary>
/// <param name="adName">查询对象的名字</param>
/// <param name="enumType">对象类别</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjPath(string adName,QueryType enumType)
{
IQueryList objGetAdsPath = (IQueryList)new ADServices();
return objGetAdsPath.GetObjPath(CONST_ADSPATH,adName,enumType);
}
#endregion
#region[对象的属性]
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="ADsPath">LDAP://地址</param>
/// <param name="adName">对象的名字</param>
/// <param name="enumType">枚举类型</param>
/// <param name="isShowValue">是否显示属性的值</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string ADsPath,string adName,QueryType enumType,bool isShowValue)
{
IQueryList objGetAttribute = (IQueryList)new ADServices();
strCollection = new StringCollection();
try
{
//取出对象的地址
StringCollection strCol = objGetAttribute.GetObjPath(@ADsPath,adName,enumType);
myEntry = new DirectoryEntry(strCol[0].ToString());
System.DirectoryServices.PropertyCollection propertyColl =myEntry.Properties;
//填充属性
foreach(string child in propertyColl.PropertyNames)
{
strCollection.Add(child);
if (isShowValue)
{
int max = myEntry.Properties[child].Count-1;
for(int i=0;i<=max;i++)
{
strCollection.Add("["+child+"值]->"+myEntry.Properties[child][i].ToString());
}
}
}
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
finally
{
myEntry.Close();
myEntry.Dispose();
}
return strCollection;
}
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="adName">对象的名字</param>
/// <param name="enumType">枚举类型</param>
/// <param name="isShowValue">是否显示属性的值</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string adName,QueryType enumType,bool isShowValue)
{
IQueryList objGetAttribute = (IQueryList)new ADServices();
return objGetAttribute.GetObjAttribute(CONST_ADSPATH,adName,enumType,isShowValue);
}
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="adName">对象的名字</param>
/// <param name="enumType">枚举类型</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string adName,QueryType enumType)
{
IQueryList objGetAttribute = (IQueryList)new ADServices();
return objGetAttribute.GetObjAttribute(CONST_ADSPATH,adName,enumType,CONST_ISOVERWRITE);
}
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="ADsPath">LDAP://地址</param>
/// <param name="adName">对象的名字</param>
/// <param name="enumType">枚举类型</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string ADsPath,string adName,QueryType enumType)
{
IQueryList objGetAttribute = (IQueryList)new ADServices();
return objGetAttribute.GetObjAttribute(ADsPath,adName,enumType,CONST_ISOVERWRITE);
}
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="ADsPath">LDAP://地址</param>
/// <param name="isShowValue">是否显示属性的值</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string ADsPath,bool isShowValue)
{
strCollection = new StringCollection();
myEntry = new DirectoryEntry(@ADsPath);
try
{
System.DirectoryServices.PropertyCollection propertyColl =myEntry.Properties;
foreach(string child in propertyColl.PropertyNames)
{
strCollection.Add(child);
if (isShowValue)
{
int max = myEntry.Properties[child].Count-1;
for(int i=0;i<=max;i++)
{
strCollection.Add("["+child+"值]->"+myEntry.Properties[child][i].ToString());
}
}
}
}
catch(Exception err)
{
strErrDescription = err.ToString();
}
return strCollection;
}
/// <summary>
/// 取出对象的属性
/// </summary>
/// <param name="ADsPath">LDAP://地址</param>
/// <returns>StringCollection</returns>
StringCollection IQueryList.GetObjAttribute(string ADsPath)
{
IQueryList objGetAttribute = (IQueryList)new ADServices();
return objGetAttribute.GetObjAttribute(ADsPath,CONST_ISOVERWRITE);
}
#endregion
#region[创建新组]
/// <summary>
/// 创建新组
/// </summary>
/// <param name="strPath">新创建组的地址 [例如:LDAP://gfg1.esquel.com/OU=Organization,DC=gfg1,DC=esquel,DC=com]</param>
/// <param name="strGroupName">新创建组的名字</param>
/// <param name="strDescription">新创建组的描述</param>
/// <param name="isOverWrite">true|fase "true"删组相同的组,"false"不删除返回false</param>
/// <returns>true|false true"组创建成功",false"组创建失败"</returns>
bool IGroup.AddGroup(string strPath, string strGroupName, string strDescription, bool isOverWrite)
{
myEntry = new DirectoryEntry(strPath);
myEntries = myEntry.Children;
//查找是否有相同的组名
try
{
DirectoryEntry entryCheck = myEntries.Find(strGroupName,"Group");
if ( entryCheck != null)
{
//如果新增的组已经存在
//isOverWrite=true|false "true"删除相同的组 "false"不删除返回falsh
if (!isOverWrite)
{
//==============*执行删除方法*================
IGroup myGroup = (IGroup)new ADServices();
if (!myGroup.DeleteGroup(strGroupName))
return false;
//============================================
}
else
{
strErrDescription = "Warning:“"+strGroupName+"” existed";
return false;
}
}
}
catch ( System.Runtime.InteropServices.COMException e2)
{
strErrDescription = "Group: " + strGroupName + " - not found. Hr = 0x" + Convert.ToString(e2.ErrorCode ,16);
}
//如果新增的组不存在
try
{
DirectoryEntry addEntry = myEntries.Add("Test0525","Group" );
addEntry.Properties ["Description"].Add(strDescription);
addEntry.CommitChanges();
addEntry.Close();
addEntry.Dispose();
return true;
}
catch (Exception err)
{
strErrDescription = err.ToString();
return false;
}
}
/// <summary>
/// 创建新组
/// </summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -