Example #1
0
        /// <summary>
        ///  在已知ou下面创建下级ou
        /// </summary>
        /// <param name="ouParentPath">LDAP://10.45.9.11/ou=离职员工,dc=ops,dc=net</param>
        /// <param name="newOu">增加ou信息(Name 必须赋值)</param>
        /// <param name="config">管理员密码配置</param>
        public bool CreateOuChilren(string ouParentPath, OuEntity newOu, AdAdminConfig config)
        {
            try
            {
                DirectoryEntry    deBase = new DirectoryEntry(ouParentPath, config.AdminAccount, config.AdminPwd);
                DirectorySearcher ouSrc  = new DirectorySearcher(deBase);
                //ouSrc.PropertiesToLoad.Add("ou");
                ouSrc.Filter      = $"(OU={newOu.Name})";
                ouSrc.SearchScope = SearchScope.Subtree;
                SearchResult srOu = ouSrc.FindOne();
                if (srOu == null)
                {
                    /* OU Creation
                     */
                    DirectoryEntry anOu = deBase.Children.Add($"OU={newOu.Name}", "organizationalUnit");
                    if (!string.IsNullOrEmpty(newOu.Descrption))
                    {
                        anOu.Properties["description"].Value = newOu.Descrption;
                    }

                    anOu.CommitChanges();
                    anOu.Close();
                    deBase.Close();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// 根据ou路径判断路径是否创建,递归创建OU tree
        /// oupath=LDAP://10.45.9.11/ou=HR,ou=离职员工,ou=allusers,dc=ops,dc=net
        /// 会创主机建出 ou=allusers,ou=离职员工,ou=hr
        /// </summary>
        /// <param name="ouPath">LDAP://10.45.9.11/ou=离职员工,dc=ops,dc=net</param>
        /// <param name="config"></param>
        /// <returns></returns>
        public bool CreateOu(string ouPath, AdAdminConfig config)
        {
            try
            {
                var paths = GetOuLevels(ouPath);
                int count = paths.Count;
                if (count < 1)
                {
                    return(false);
                }
                var notExistsIndex = count - 1;
                var currentPath    = paths[notExistsIndex];
                if (!IsExistsOu(currentPath, config))
                {
                    GetExistsLevalIndex(paths, config, ref notExistsIndex);
                    var parentOuPath = paths[notExistsIndex];
                    for (int i = notExistsIndex + 1; i < count; i++)
                    {
                        var itemPath = paths[i];
                        var ouName   = GetLastOuName(itemPath);
                        var ouEntity = new OuEntity {
                            Name = ouName
                        };
                        var isSuccess = CreateOuChilren(parentOuPath, ouEntity, config);
                        if (!isSuccess)
                        {
                            return(false);
                        }

                        parentOuPath = itemPath;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
                return(false);
            }
        }