I have a process for users to update inofrmation in Ad, one of the things I never accounted for was last name change. I am trying to change the CN but it keeps erroring me out and tried to search for information but have not been able to find anything.
Does anyone know how to update the CN of an Active Directory account. It does not like the remaning of the CN, others said this should work but does not seem to work for me.
Here is my code.
myDataTable = New DataTable
myDataTable = getData(sql)
'Build the Cn
cn = myDataTable.Rows(0)(3).ToString() & "\, " & myDataTable.Rows(0)(17).ToString()
If myDataTable(0)(11) <> "" Then
phone = myDataTable(0)(11)
Else
phone = " "
End If
If myDataTable(0)(12) <> "" Then
ip = myDataTable(0)(12)
Else
ip = " "
End If
Try
user.Properties("employeeType").Value = myDataTable(0)(0).ToString()
user.Properties("mail").Value = myDataTable(0)(1).ToString()
user.Properties("displayName").Value = myDataTable(0)(2).ToString()
user.Properties("sn").Value = myDataTable(0)(3).ToString()
user.Properties("personalTitle").Value = myDataTable(0)(4).ToString()
user.Properties("title").Value = myDataTable(0)(5).ToString()
user.Properties("department").Value = myDataTable(0)(6).ToString()
user.Properties("l").Value = myDataTable(0)(7).ToString()
user.Properties("streetAddress").Value = myDataTable(0)(8).ToString()
user.Properties("st").Value = myDataTable(0)(9).ToString()
user.Properties("postalCode").Value = myDataTable(0)(10)
user.Properties("telephoneNumber").Value = phone.ToString()
user.Properties("ipPhone").Value = ip.ToString
user.Properties("physicalDeliveryOfficeName").Value = myDataTable.Rows(0)(13).ToString()
user.Properties("extensionAttribute2").Value = myDataTable.Rows(0)(14).ToString()
user.Properties("comment").Value = myDataTable.Rows(0)(15).ToString()
user.Properties("description").Value = myDataTable.Rows(0)(16).ToString()
user.CommitChanges()
'user.Rename("CN=" & cn)
'user.CommitChanges()
Return True
Catch ex As Exception
Return False
End Try
user.Close()
End If
dirEntry.Close()
I don't think you should need the domain here. Assuming that column 17 is the user name then it would be:
User.Rename("CN=" + myDataTable.Rows(0)(17).ToString())
HI, that is not the domain, it is the last name and then I add some other data to it. Here is the error I receive.
A device attached to the system is not functioning.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.DirectoryServices.DirectoryServicesCOMException: A device attached to the system is not functioning.
Source Error:
Line 320: user.Properties("description").Value = myDataTable.Rows(0)(16).ToString()
Line 321: user.CommitChanges()
Line 322: user.Rename("CN=" + cn)
Line 323: 'user.CommitChanges()
Line 324: ' End With
Source File: C:\inetpub\wwwroot\iMAC\App_Code\iMACAD.vb Line: 322
Stack Trace:
[DirectoryServicesCOMException (0x8007001f): A device attached to the system is not functioning.
]
System.DirectoryServices.DirectoryEntry.MoveTo(DirectoryEntry newParent, String newName) +335
System.DirectoryServices.DirectoryEntry.Rename(String newName) +44
iMACAD.UpdateADUser(String samAccount, Int32 userId) in C:\inetpub\wwwroot\iMAC\App_Code\iMACAD.vb:322
Admin_MacRequests.lnkSubmit_Click(Object sender, EventArgs e) in C:\inetpub\wwwroot\iMAC\Admin\MacRequests.aspx.vb:366
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +116
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +101
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9528578
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Oh.... I have pretty much the same code but I don't have the / in my names. Perhaps you need to escape it: // or try a different character.
HI I don't either I thought the / was the escape to get the comma puit in between last and first names.
Doe, John H MN NG FTS
Did you try without it?
found out i can conetacting and extra comma that i was not escaping in my datarow. Guess I should hve checked the data before starting a fuss.
沒有留言:
張貼留言