具有字母数字的名称/地址的AD帐户创建失败

时间:2018-07-17 18:26:56

标签: c# exchange-server

在使用.NET的引用/命名空间(即System.DirectoryServices)对Active Directory域服务进行查询时,我们无法创建在显示名称或电子邮件地址中包含数字的AD帐户。这是一个两步过程,创建邮箱,然后更新所有其他属性。在创建邮箱时,我们没有收到任何错误,但是在将属性更新到同一邮箱时,由于没有邮箱/ sAMAccountName,因此我们遇到了自定义错误。

我们在整个代码中使用相同的DC。此外,如果建议的显示名称和电子邮件地址没有数字,则代码可以正常工作。 由于Exchange 2016服务器已升级到CU8,因此我们面临此问题。 我们使用.NET代码而不是Exchange来避免人工干预。它与其他系统集成在一起,可以自动生成邮箱和AD帐户。 这是一个两步过程,因为我们无法将所有其他属性传递给New-Mailbox cmdlet。创建邮箱后,我们将使用其他属性对其进行更新。

在事件查看器中,对象已创建,修改并最终删除。删除是自动发生的,这是例外。

代码段

//Creating mailbox
DirectorySearcher dSearchUPN = new DirectorySearcher(deCon);
dSearchUPN.Filter = "(UserPrincipalName=" + strUserPrincipalName + ")";
SearchResult sResultUPN = dSearchUPN.FindOne();
if (sResultUPN == null)
{
                                //Open it
                                runspace.Open();

                                using (PowerShell powershell = PowerShell.Create())
                                {
                                    powershell.Runspace = runspace;
                                    powershell.AddCommand("New-Mailbox");

                                    powershell.AddParameter("Name", strName);
                                    powershell.AddParameter("UserPrincipalName", strUserPrincipalName);
                                    powershell.AddParameter("Alias", strAlias);
                                    powershell.AddParameter("PrimarySmtpAddress", strPrimarySmtpAddress);
                                    powershell.AddParameter("OrganizationalUnit", strOrganizationalUnit);
                                    powershell.AddParameter("Database", strDatabase);
                                    powershell.AddParameter("SamAccountName", strSamAccountName);
                                    powershell.AddParameter("Password", strPassword);
                                    powershell.AddParameter("ResetPasswordOnNextLogon", true);
                                    powershell.AddParameter("DomainController", "ABCDWXYZADC2.corp.cyient.com");
                                    powershell.Invoke();
}

//Logic for Display Name & Email Address for duplicate First Name & Last Name
int i = 1;

while (!string.IsNullOrEmpty(searchEmailAddress(strPrimarySmtpAddress)))
{
i = i + 1;

strName = dr["firstName"].ToString().Replace(".", "") + " " + dr["lastName"].ToString().Replace(".", "") + " " + dr["workerID"].ToString();

strPrimarySmtpAddress = dr["firstName"].ToString().Replace(" ", "").Replace(".", "") + "." + dr["lastName"].ToString().Replace(" ", "").Replace(".", "") + i.ToString() + strSMTPDomain;
}

//Updating mailbox

DirectorySearcher dSearch;

                dSearch = new DirectorySearcher(deCon);

                //Filtering AccountName with Database Domain 
                dSearch.Filter = "(sAMAccountName=" + strSamAccountName + ")";
                SearchResult sResult = dSearch.FindOne();
                if (sResult != null)    //Check if the Account is available
                {
                    DirectoryEntry deToUpdate = sResult.GetDirectoryEntry();

                    if (!string.IsNullOrEmpty(strEmployeeNumber))
                    {
                        deToUpdate.Properties["employeeNumber"].Value = strEmployeeNumber;
                        strToDisplay = strToDisplay + "\r\nEmployee Number: " + strEmployeeNumber;
                    }
                    if (!string.IsNullOrEmpty(strEmployeeType))
                    {
                        deToUpdate.Properties["employeeType"].Value = strEmployeeType;
                        strToDisplay = strToDisplay + "\r\nEmployee Type: " + strEmployeeType;
                    }
                    if (!string.IsNullOrEmpty(strGivenName))
                    {
                        deToUpdate.Properties["givenName"].Value = strGivenName;
                        strToDisplay = strToDisplay + "\r\nFirst Name: " + strGivenName;
                    }
                    if (!string.IsNullOrEmpty(strSurName))
                    {
                        deToUpdate.Properties["sn"].Value = strSurName;
                        strToDisplay = strToDisplay + "\r\nLast Name:" + strSurName;
                    }

                    if (!string.IsNullOrEmpty(strTitle))
                    {
                        deToUpdate.Properties["title"].Value = strTitle;
                        strToDisplay = strToDisplay + "\r\nTitle: " + strTitle;
                    }
                    if (!string.IsNullOrEmpty(strCompany))
                    {
                        deToUpdate.Properties["company"].Value = strCompany;
                        strToDisplay = strToDisplay + "\r\nCompany: " + strCompany;
                    }
                    if (!string.IsNullOrEmpty(strDepartment))
                    {
                        deToUpdate.Properties["department"].Value = strDepartment;
                        strToDisplay = strToDisplay + "\r\nDepartment: " + strDepartment;
                    }
                    if (!string.IsNullOrEmpty(strBUDesc))
                    {
                        deToUpdate.Properties["physicalDeliveryOfficeName"].Value = strBUDesc;
                        strToDisplay = strToDisplay + "\r\nBusiness Unit: " + strBUDesc;
                    }
                    if (!string.IsNullOrEmpty(strStreetAddress))
                    {
                        deToUpdate.Properties["streetAddress"].Value = strStreetAddress;
                        strToDisplay = strToDisplay + "\r\nAddress: " + strStreetAddress;
                    }
                    if (!string.IsNullOrEmpty(strLocation))
                    {
                        deToUpdate.Properties["l"].Value = strLocation;
                        strToDisplay = strToDisplay + "\r\nCity: " + strLocation;
                    }
                    if (!string.IsNullOrEmpty(strState))
                    {
                        deToUpdate.Properties["st"].Value = strState;
                        strToDisplay = strToDisplay + "\r\nState:" + strState;
                    }
                    if (!string.IsNullOrEmpty(strPostalCode))
                    {
                        deToUpdate.Properties["postalCode"].Value = strPostalCode;
                        strToDisplay = strToDisplay + "\r\nZip Code: " + strPostalCode;
                    }
                    if (!string.IsNullOrEmpty(strCountry))
                    {
                        deToUpdate.Properties["c"].Value = strCountry;
                        strToDisplay = strToDisplay + "\r\nCountry/Region:" + strCountry + "";
                    }
                    if (!string.IsNullOrEmpty(strExpiryDate))
                    {
                        DateTime dtExpiryDate = Convert.ToDateTime(strExpiryDate).AddDays(1);

                        deToUpdate.Properties["accountExpires"].Value = Convert.ToString((Int64)dtExpiryDate.ToFileTime());
                        strToDisplay = strToDisplay + "\r\nExpiry Date:" + strExpiryDate + "";
                    }

                    if (!string.IsNullOrEmpty(strExtensionAttribute1))
                        deToUpdate.Properties["extensionAttribute1"].Value = strExtensionAttribute1;    //Update Company Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute2))
                        deToUpdate.Properties["extensionAttribute2"].Value = strExtensionAttribute2;    //Update Location Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute3))
                        deToUpdate.Properties["extensionAttribute3"].Value = strExtensionAttribute3;    //Update Job Title Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute4))
                        deToUpdate.Properties["extensionAttribute4"].Value = strExtensionAttribute4;    //Update Management Level Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute8))
                        deToUpdate.Properties["extensionAttribute8"].Value = strExtensionAttribute8;    //Update Sup Org Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute10))
                        deToUpdate.Properties["extensionAttribute10"].Value = strExtensionAttribute10;    //Update Department Code
                    if (!string.IsNullOrEmpty(strExtensionAttribute13))
                        deToUpdate.Properties["extensionAttribute13"].Value = strExtensionAttribute13;    //Update Sup Org

                        }
                        catch (Exception exObj)
                        {
                            WriteToErrorLog("Error while updating Manager details.\n", " Emp AD ID: " + strAlias + "; Manager ID: " + strManager, "Application Exception");
                            WriteToErrorLog("Error while updating Manager (" + strManager + ") details of the Employee (" + strAlias + ").\n", "Message: " + exObj.Message + ";\n Method Name: " + exObj.TargetSite, "Application Exception");
                        }
                    }

                    deToUpdate.CommitChanges();

                    WriteToLog("Account updation succeeded...", "AD Attributes: " + strToDisplay);
                }
else
                {
                    WriteToLog("Account creation/updation failed...", "AD Attributes: " + strToDisplay);
                    strStatusCode = "0";
                    strStatusDesc = "Account Creation failed.";
                }

0 个答案:

没有答案