Example #1
0
        public AdEntity GetInstance(PSObject psObject)
        {
            var adentity = new AdEntity();

            if (psObject.Properties["SamAccountName"] != null)
            {
                adentity.SamAccountName = psObject.Properties["SamAccountName"].Value.ToString();
            }
            else
            {
                return(null);
            }

            if (psObject.Properties["Phone"] != null)
            {
                adentity.Phone = psObject.Properties["Phone"].Value.ToString();
            }

            if (psObject.Properties["LastName"] != null)
            {
                adentity.LastName = psObject.Properties["LastName"].Value.ToString();
            }

            if (psObject.Properties["FirstName"] != null)
            {
                adentity.FirstName = psObject.Properties["FirstName"].Value.ToString();
            }

            if (psObject.Properties["Name"] != null)
            {
                adentity.Name = psObject.Properties["Name"].Value.ToString();
            }

            if (psObject.Properties["Description"] != null)
            {
                adentity.Description = psObject.Properties["Description"].Value.ToString();
            }

            if (psObject.Properties["DisplayName"] != null)
            {
                adentity.DisplayName = psObject.Properties["DisplayName"].Value.ToString();
            }

            if (psObject.Properties["Alias"] != null)
            {
                adentity.Alias = psObject.Properties["Alias"].Value.ToString();
            }

            if (psObject.Properties["UserPrincipalName"] != null)
            {
                adentity.UserPrincipalName = psObject.Properties["UserPrincipalName"].Value.ToString();
            }

            if (psObject.Properties["OrganizationalUnit"] != null)
            {
                adentity.OrganizationalUnit = psObject.Properties["OrganizationalUnit"].Value.ToString();
            }

            return(adentity);
        }
        /// <summary>
        /// 创建用户邮箱帐号
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static ExcuseResult CreateUserMailBox(AdEntity entity, ExchangeAdminConfig config)
        {
            ExcuseResult er      = new ExcuseResult();
            var          command = new Command("Enable-Mailbox");

            command.Parameters.Add("Identity", entity.SamAccountName);
            command.Parameters.Add("Alias", entity.Alias);
            command.Parameters.Add("Database", entity.Database);
            command.Parameters.Add("Password", String2SecureString(entity.Password));
            if (!string.IsNullOrEmpty(config.DomainController))
            {
                command.Parameters.Add("DomainController", config.DomainController);
            }

            try
            {
                string result = RunExchangePowerShellScript(command, config);
                if (!string.IsNullOrEmpty(result) && result != "FAIL")
                {
                    return(er.SetSuccessResult());
                }

                return(er.SetFailResult());
            }
            catch (Exception ex)
            {
                return(er.SetErrorResult(ex.Message));
            }
        }
        /// <summary>
        /// 获取用户组成员
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="exConfig"></param>
        /// <returns></returns>
        public static List <AdEntity> GetDistributionGroupMember(string identity, ExchangeAdminConfig exConfig)
        {
            List <AdEntity> users   = new List <AdEntity>();
            var             command = new Command("Get-DistributionGroupMember");

            command.Parameters.Add("Identity", identity);
            try
            {
                IEnumerable <PSObject> menbers;
                string result = RunExchangePowerShellScript(command, exConfig, out menbers);
                if (!string.IsNullOrEmpty(result) && result != "FAIL")
                {
                    foreach (var item in menbers)
                    {
                        PSObject psObject = item;
                        var      user     = new AdEntity().GetInstance(psObject);
                        if (user != null)
                        {
                            users.Add(user);
                        }
                    }

                    return(users);
                }

                return(users);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        /// <summary>
        /// 创建AD帐号并开通Exchange帐号
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static ExcuseResult CreateAdAccountAndOpenExcahnge(AdEntity entity, ExchangeAdminConfig config)
        {
            ExcuseResult er      = new ExcuseResult();
            var          command = new Command("New-Mailbox");

            command.Parameters.Add("Name", entity.Name);
            command.Parameters.Add("Alias", entity.Alias);
            command.Parameters.Add("UserPrincipalName", entity.UserPrincipalName + "@" + entity.Ldap);
            command.Parameters.Add("SamAccountName", entity.SamAccountName);
            command.Parameters.Add("FirstName", entity.FirstName);
            command.Parameters.Add("LastName", entity.LastName);
            command.Parameters.Add("Password", String2SecureString(entity.Password));
            command.Parameters.Add("ResetPasswordOnNextLogon", false);
            command.Parameters.Add("OrganizationalUnit", $"{entity.Ldap}/{entity.OrganizationalUnit}");
            command.Parameters.Add("Database", entity.Database);
            command.Parameters.Add("DisplayName", entity.DisplayName);

            if (!string.IsNullOrEmpty(config.DomainController))
            {
                command.Parameters.Add("DomainController", config.DomainController);
            }

            try
            {
                string result = RunExchangePowerShellScript(command, config);
                if (!string.IsNullOrEmpty(result) && result != "FAIL")
                {
                    return(er.SetSuccessResult());
                }

                return(er.SetFailResult());
            }
            catch (Exception ex)
            {
                return(er.SetErrorResult(ex.Message));
            }
        }