Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to Modify the CN, NAME, and DN

Status
Not open for further replies.

yellowartist

IS-IT--Management
Aug 5, 2007
19
0
0
US
Is it possible using VBScript to modify the CN, Name, and Distinguished name for a list of users in Active Directory.

I am trying to write a script to use when we separate employees. The standard now is to put the date they left infront of their name in active directory. I can get the display name to change but it is not changing it when browing ADUC for it.

Please assist...

Here is my script so far but I get an error every time....

The error is:
Error: "The directory service cannot perform the requested operation on the RDN attribute of an object."
"Code: 8072016


**Note** I have modified some information to protect company information**

Thanks for any assistance!
****************************************
Option Explicit
'on error resume next

Dim objUser, objShell
Dim objExcel, objSpread, intRow, intCol
Dim strUser, strSheet, strDate
Dim strCN, strSam, strDisplay, strName, strDN
Dim strmail, strUPN, strextA5
Dim objFSO, objFolder, objFileShare, strDest, strougroup, objgroup
Dim Inputprompt, strsn

strSheet = "path to spreadsheet"


' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 1

' Here is the 'DO...Loop' that cycles through the cells

' Note intRow, x must correspond to the column in strSheet

Do Until objExcel.Cells(intRow,1).Value = ""
strCN = Trim(objExcel.Cells(intRow, 1).Value)
strSam = Trim(objExcel.Cells(intRow, 2).Value)
strDisplay = Trim(objExcel.Cells(intRow, 3).Value)
strName = Trim(objExcel.Cells(intRow, 4).Value)
strDN = Trim(objExcel.Cells(intRow, 5).Value)
strmail = Trim(objExcel.Cells(intRow, 6).Value)
strextA5 = Trim(objExcel.Cells(intRow, 7).Value)
strUPN = Trim(objExcel.Cells(intRow, 8).Value)
strsn = Trim(objExcel.Cells(intRow, 10).Value)
strDate = "08/01/07 "
strougroup = "Group"
'Build the actual User from data in strSheet.

'Const ADS_PROPERTY_APPEND = 3

Set objUser = GetObject("LDAP://" & strdn)
'Wscript.echo "LDAP://" & strdn
objUser.Put "sAMAccountName", ("1" & strSam)
objUser.Put "DisplayName", (strDate & strdisplay)
objUser.Put "sn", (strsn)
objUser.Put "userPrincipalName", ("1" & strUPN)
objUser.Put "extensionAttribute5", "EXEMPT"
objUser.SetPassword "i5A2sj*!"
objuser.setInfo

' objUser.Put "cn", (strDate & strcn)
' objUser.Put "Name", (strDate & strname)
' objUser.Put "distinguishedName", (strDate & strdn)
objuser.setInfo

'add new Primary Address
objuser.PutEx 3, "proxyAddresses", Array("smtp:" & "del" & strmail)
objuser.setInfo
'Delete Old Primary
objuser.PutEx 4, "ProxyAddresses", Array("SMTP:" & strmail)
objuser.Put "mail", ("del" & strmail)
objuser.setInfo

'Hide From GAL
' objuser.put "msExchangeHideFromAddressLists", True
objUser.SetInfo

' Add to Group
If strougroup = False Then
Set objgroup = getobject("LDAP://" & Strougroup)
objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(strdn)
objGroup.SetInfo
End If

' Increment to next user.
intRow = intRow + 1
Loop

Wscript.Echo "Done"
objExcel.Quit
WScript.Quit
 
[1]
>Is it possible using VBScript to modify the CN, Name, and Distinguished name for a list of users in Active Directory.
You can but not via .put "cn" etc like you are using... (see [3]).

[2]
>' objUser.Put "cn", (strDate & strcn)
>' objUser.Put "Name", (strDate & strname)
>' objUser.Put "distinguishedName", (strDate & strdn)

[2.1] Methodologically, it is incorrect.
[2.2] First let's not involve ourselves in the method. You intend to change the cn from old cn (strcn) to strDate & strcn. You will get yourself into unnecessary trouble. The slash (/) is a restricted character to appear in the component's value of distinguished name. It has to be escaped like by replacing it to "\/". (Why make more trouble to those who maintain the directory if they are not yet fluent in it?)
[2.3] In the .put "distinguishedname" line, the method is worng-that's said. But do you notice what strDate & strdn looks like? "08/01/07cn=p,ou=q,dc=r,dc=s..." does it look like a distinguished name?

[3] You have to postpone changing cn and dn to the last part of the script after doing the rest. Hence continue to comment out those two lines.

[3.1] Uncomment the line to change "name" at its place. (In principle, it is not forbidden to do it there.)
[tt] objUser.Put "Name", (strDate & strname[/tt]

[3.2] At the end of the script, start change the cn; and the change of the distinguishedname is a consequence of it. There is not need to change it by itself, not repeating what already said that you can't.
[tt]
'objUser.Put "cn", (strDate & strcn)
'objUser.Put "distinguishedName", (strDate & strdn)
GetObject(objUser.parent).moveHere objUser.adspath,"cn=" & replace(strDate,"/","\/") & strcn
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top