我正在尝试以编程方式重命名用户,但无法找出邮箱(proxyAddresses)。任何帮助表示赞赏...
以下工作代码......
Public Shared Function renameUser(ByVal curUsername As String, ByVal newUsername As String) As Boolean
Dim emailSuffix As String = "@here.com"
Dim userPrincipalSuffix As String = "@here.now"
Dim user As New DirectoryEntry
Dim oSearcher As DirectorySearcher = Nothing
Dim oRoot As DirectoryEntry = Nothing
Dim oResult As SearchResult
Try
oRoot = New DirectoryEntry("LDAP://" & "ldapserver" & _
"/" & "OU=OUWithUsersToChange,OU=Site Users,DC=here,DC=now")
oSearcher = New DirectorySearcher(oRoot)
oSearcher.SearchScope = SearchScope.Subtree
oSearcher.Filter = "(&(objectCategory=person)(sAMAccountName=" & curUsername & "))"
oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("mail")
oSearcher.PropertiesToLoad.Add("mailNickname")
oSearcher.PropertiesToLoad.Add("userPrincipalName")
oSearcher.PropertiesToLoad.Add("sAMAccountName")
oSearcher.PropertiesToLoad.Add("proxyAddresses")
oSearcher.PropertiesToLoad.Add("textEncodedORAddress")
oSearcher.PropertiesToLoad.Add("legacyExchangeDN")
oResult = oSearcher.FindOne
user = oResult.GetDirectoryEntry
Dim lNewList As New List(Of String)
For Each sAddress As String In user.Properties("proxyAddresses")
lNewList.Add(sAddress.Replace(curUsername, newUsername))
Next
Dim sTextEncodedORAddress As String = user.Properties.Item("textEncodedORAddress").Value
Dim sLegacyExchangeDN As String = user.Properties.Item("legacyExchangeDN").Value
user.Properties.Item("uid").Value = newUsername
user.Properties.Item("mail").Value = newUsername & emailSuffix
user.Properties.Item("mailNickname").Value = newUsername
user.Properties.Item("userPrincipalName").Value = newUsername & userPrincipalSuffix
user.Properties.Item("sAMAccountName").Value = newUsername
user.Properties("proxyAddresses").Value = lNewList.ToArray
user.Properties.Item("textEncodedORAddress").Value = sTextEncodedORAddress.Replace(curUsername, newUsername)
user.Properties.Item("legacyExchangeDN").Value = sLegacyExchangeDN.Replace(curUsername, newUsername)
user.CommitChanges()
user.Rename("CN=" & newUsername)
Return True
Catch ex As Exception
Return False
Finally
user.Dispose()
oRoot.Dispose()
oSearcher.Dispose()
oResult = Nothing
End Try
End Function
答案 0 :(得分:1)
如果我没记错的话,proxyAddresses实际上是一个数组,而不是单个值。它还带有关于它的地址类型的信息...
警告:未来的代码很难!
这是我之前使用的一些代码,因此更改了用户帐户的主电子邮件地址并保留以前的地址(如切换主域名)。这可能有助于您进行更改
Dim lNewList As New List(Of String)
sPrimaryAddress = sPrimaryAddress.Split("@")(0) & "@" & "example.com"
lNewList.Add("SMTP:" & sPrimaryAddress)
For Each sAddr As String In lPrevList
lNewList.Add(sAddr) 'which will be a list of values like "smtp:someone@domain.com"'
Next
Dim oUser As DirectoryEntry = oResult.GetDirectoryEntry()
oUser.Properties("mail").Value = sPrimaryAddress
oUser.Properties("ProxyAddresses").Value = lNewList.ToArray()
oUser.CommitChanges()
lPrevList 是用户已附加到其个人资料的电子邮件地址列表。它们的主要地址以 SMTP:开头,而其他地址以 smtp:(小写)开头。您可能会运行其他值,例如 x400 等。
请务必小心处理每个值。您不想编写脚本然后在您的域中运行它并炸掉所有帐户
答案 1 :(得分:0)
如果您使用的是.Net Framework 3.5或更高版本,那么最好使用System.DirectoryServices.AccountManagement。这使得获取正确的UserPrinciple,更改Name属性和调用Save方法成为一个问题。它可能看起来像这样:
Dim pc As New PrincipalContext(ContextType.Domain, "COMPANY")
Dim up As UserPrincipal = UserPrincipal.FindByIdentity(pc, curUsername)
up.Name = newUsername
up.Save