Re: Mass Username Change-How do I do it?
Randy wrote:
> We have about 350 end users on our Windows Server 2003 network. All
> clients are XP SP2. Our present username format is first initial, middle
> initial, lastname. Is there a way to change that format to
> firstname.lastname? I don't want to have to change all of them manually.
It can be done with a VBScript program. Perhaps several command line tools
could be used as well. The first thing is to clarify what is to be changed.
Is it the "Common Name", the NT Name (also called the "Windows 2000 logon
name"), or the display name?
The Common Name must be unique in the container/OU. You must rename the
object to change it. The NT Name must be unique in the domain. It can be
modified directly. Also, are the first name and last name fields in ADUC
filled in? Otherwise, we need some source for the value of firstname.
Assuming you are changing the NT Names and that the first name and last name
fields are filled in, a VBScript program to modify all users in AD
(including Administrator, Guest, etc.) could use ADO to retrieve the values
for all users, then bind to each user object and modify. For example:
================
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN
Dim objUser, strFirst, strLast
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on all user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,givenName,sn"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strDN = adoRecordset.Fields("distinguishedName").Value
strFirst = adoRecordset.Fields("givenName").Value & ""
strLast = adoRecordset.Fields("sn").Value & ""
' Skip users with either first or last name missing.
' This will probably skip all built-in user accounts.
If (strFirst <> "") And (strLast <> "") Then
' Bind to user object.
Set objUser = GetObject("LDAP://" & strDN)
' Assign new NT name (pre-Windows 2000 logon name).
objUser.sAMAccountName = strFirst & "." & strLast
' Save changes. Trap error in case this is a duplicate name.
On Error Resume Next
objUser.SetInfo
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "User " & objUser.sAMAccountName _
& " cannot be renamed " & strFirst & "." & strLast
End If
' Restore normal error handling.
On Error GoTo 0
End If
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
===========
This script could be limited to a specified OU by modifying the base of the
query. For example, to run the script just for the the users in
"ou=TestOU,dc=MyDomain,dc=com":
strBase = "<LDAP://ou=TestOU,dc=MyDomain,dc=com>"
For more one using ADO in VBScript programs see this link:
http://www.rlmueller.net/ADOSearchTips.htm
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab -
http://www.rlmueller.net
--