Script for changing user logon names in AD to all lower case

Discussion in 'Scripting' started by Dolfan849, May 14, 2008.

  1. Dolfan849

    Dolfan849 Guest

    Can anyone help me with a script to change all user logon names in W2k3 AD to
    lowercase? I currently have usernames with some uppercase characters and
    need to have them all changed to lower case. Any help would be greatly
    appreciated.

    Thanks,
    Terry Miller
     
    Dolfan849, May 14, 2008
    #1
    1. Advertisements

  2. You should use VBScript built in function LCase.
    It returns a string converted to lowercase.


    Regards,
    Vedran Matica
     
    Vedran Matica, May 14, 2008
    #2
    1. Advertisements

  3. Dolfan849

    Wiseman82 Guest

    You might find my Bulk Modify app useful:

    http://www.wisesoft.co.uk/Products/PasswordControl/BulkModify/Default.aspx

    Using the XML placeholders you can convert usernames to lowercase. In this
    case you would enter the following text into the username (Pre Windows 2000)
    textbox:

    <attr transform="LOWER">sAMAccountName</attr>

    Or this in the user logon name (User Principal Name) textbox if you want to
    change the user principal name ():

    <attr transform="LOWER">userPrincipalName</attr>

    Hope this helps,

    David
    http://www.wisesoft.co.uk
    (My personal website and a free information resource for IT Professionals)
     
    Wiseman82, May 14, 2008
    #3
  4. First, I assume you mean the "pre-Windows 2000 logon name", which is the
    value of the sAMAccountName attribute. Next, names in AD are not case
    sensitive. You can logon (or bind to or find) using any case (or a mixed
    case).

    A VBScript program can use ADO to retrieve attribute values for all users.
    You would need to bind to each user object to make changes. An example that
    only modifies the user if necessary follows:
    ===========
    Option Explicit

    Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

    Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strNTName

    Dim strUserDN, objUser



    ' 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 user objects.
    strFilter = "(&(objectCategory=person)(objectClass=user))"



    ' Comma delimited list of attribute values to retrieve.
    strAttributes = "sAMAccountName,distinguishedName"



    ' 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 and display.
    strNTName = adoRecordset.Fields("sAMAccountName").Value

    strUserDN = adoRecordset.Fields("distinguishedName").value

    ' Make sAMAccountName all lower case.

    strNewName = LCase(strNTName)

    ' Check if sAMAccountName should be modified.

    If (strNTName <> strNewName) Then

    ' Bind to the user object.

    Set objUser = GetObject("LDAP://" & strUserDN)

    ' Assign new value for sAMAccountName.

    objUser.sAMAccountName = strNewName

    ' Save changes.

    objUser.SetInfo

    End If

    ' Move to the next record in the recordset.
    adoRecordset.MoveNext
    Loop



    ' Clean up.

    adoRecordset.Close

    adoConnection.Close
     
    Richard Mueller [MVP], May 14, 2008
    #4
  5. Dolfan849

    stm_tech

    Joined:
    Mar 26, 2012
    Messages:
    1
    Likes Received:
    0
    Need to set UPN as well

    Ok so using the script above, I have modified it for the UPN, would this be ok?

    Option Explicit

    Dim adoCommand, adoConnection, strBase, strFilter, strAttributes

    Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strNTName, strUPN

    Dim strUserDN, objUser

    ' 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 user objects.
    strFilter = "(&(objectCategory=person)(objectClass=user))"



    ' Comma delimited list of attribute values to retrieve.
    strAttributes = "sAMAccountName,distinguishedName,userPrincipalName"



    ' 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 and display.
    strNTName = adoRecordset.Fields("sAMAccountName").Value

    strUserDN = adoRecordset.Fields("distinguishedName").value

    strUPN = adoRecordset.Fields("userPrincipalName").value

    ' Make sAMAccountName all lower case.

    strNewName = LCase(strNTName)

    ' Make userPrincipleName all lower case.

    strNewUPN = LCase(strUPN)

    ' Check if sAMAccountName should be modified.

    If (strNTName <> strNewName) Then

    ' Bind to the user object.

    Set objUser = GetObject("LDAP://" & strUserDN)

    ' Assign new value for sAMAccountName.

    objUser.sAMAccountName = strNewName

    ' Save changes.

    objUser.SetInfo

    End If

    ' Check if userPrincipleName should be modified.

    If (strUPN <> strNewUPN) Then

    ' Bind to the user object.

    Set objUser = GetObject("LDAP://" & strUserDN)

    ' Assign new value for userPrincipleName

    objUser.userPrincipleName = strNewUPN

    ' Save changes.

    objUser.SetInfo

    End If
    ' Move to the next record in the recordset.
    adoRecordset.MoveNext
    Loop

    ' Clean up.

    adoRecordset.Close

    adoConnection.Close
     
    stm_tech, Mar 26, 2012
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.