script is invoked from a batch file but code is:
rem chgdisplay.vbs - Changes the display names of all users in a given OU to the
rem format of Lastname, Firstname.
rem Usage = cscript chgdisplay.vbs "OU=My Ou, DC=My Domain, DC=com"
rem OU must be enclosed in quotes if it contains spaces in the name
'
Dim strTargetOU
ParseCommandLine()
wscript.echo strTargetOU
wscript.echo
wscript.echo "Changing Display names of users in " & strTargetOU
Set oTargetOU = GetObject("LDAP://" & strTargetOU)
oTargetOU.Filter = Array("user")
For each usr in oTargetOU
if instr(usr.SamAccountName, "$") = 0 then
On Error Resume Next
vLast = usr.get("Sn")
vFirst = usr.get("GivenName")
On Error GoTo 0
If (vLast = "") And (vFirst = "") Then
vFullName = usr.sAMAccountName
ElseIf (vLast = "") Then
vFullName = vFirst
ElseIf (vFirst = "") Then
vFullName = vLast
Else
vFullname = vLast & ", " & vFirst
End If
usr.put "displayName", vFullName
usr.setinfo
wscript.echo usr.displayName
end if
Next
For each usr in oTargetOU
if instr(usr.SamAccountName, "$") = 0 then
On Error Resume Next
vLast = usr.get("Sn")
vFirst = usr.get("GivenName")
On Error GoTo 0
If (vLast = "") And (vFirst = "") Then
vFullName = usr.sAMAccountName
ElseIf (vLast = "") Then
vFullName = vFirst
ElseIf (vFirst = "") Then
vFullName = vLast
Else
vFullname = vLast & ", " & vFirst
End If
On Error Resume Next
oTargetOU.MoveHere usr.AdsPath, "cn=" & vFullName
'Set oNewUser = oTargetOU.MoveHere(usr.AdsPath, "cn=" & vFullName)
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to rename " & usr.distinguishedName
Else
On Error GoTo 0
Wscript.Echo "Renamed " & usr.distinguishedName
End If
end if
Next
Sub ParseCommandLine()
Dim vArgs
set vArgs = WScript.Arguments
if vArgs.Count <> 1 then
DisplayUsage()
Else
strTargetOU = vArgs(0)
End if
End Sub
Sub DisplayUsage()
WScript.Echo
WScript.Echo "Usage: cscript.exe " & WScript.ScriptName & " <Target OU to change users display names in>"
WScript.Echo "Example: cscript " & WScript.ScriptName & " " & chr(34) & "OU=Test OU,DC=ubht,DC=nhs,DC=uk" & chr(34)
WScript.Quit(0)
End Sub
it runs but says unable to rename CN=firstname lastname,OU=test,DC=domain,DC=com
i have the movetree.exe file in same folder.