RDN Modify Script

' #####################################################################
' # RDN Modify
' # Program that uses that replaces the RDN with the users display name.
' # It can also fill in first and last names.
' #
' # Make sure you specify the Path so that it looks in the right place.
' ######################################################################


<job id="RDN Modify">
<script language="VBScript">

Function CorrectRDN(Parent, ADsPath, newCN)

WScript.Echo currUser & " " & newCN
Set objParent = GetObject(Parent)
objParent.MoveHere ADsPath, newCN

End Function


Parent = "LDAP://OU=Users,DC=sample,DC=com" ' ## Change this to whatever ou you want to find user accounts in
Set objUserOU = getobject(Parent)

i = 0
for each colUser in objUserOU

If InStr(colUser.displayName, ",") > 0 Then


Pos = InStr(4,colUser.distinguishedName, "=")
RDN = Mid(colUser.distinguishedName, 4,Pos-7) ' start at the fourth to skip the CN=

Wscript.Echo colUser.displayName & " " & colUser.sAMAccountName
commaPos = InStr(1,colUser.displayName, ",")
lastName = Left(colUser.displayName, commaPos-1)
firstName = Right(colUser.displayName, Len(colUser.displayName)-commaPos)

if RDN = colUser.sAMAccountName Then
newCN = "CN=" & lastName & "\," & firstName
CorrectRDN Parent, colUser.ADsPath, newCN
i=i+1
End If


' ################################################
' Update the first and first and last names

If colUser.givenName = "" Then
WScript.Echo Trim(firstName)
colUser.Put "givenName", Trim(firstName)
End If
If colUser.SN = "" Then
WScript.Echo Trim(lastName)
colUser.Put "SN", lastName
End If
colUser.SetInfo
' ################################################

End If

Next


Wscript.Echo i & " Users found with matching username and relative distinguished name."

</script>
</job>