+ Post New Thread
Results 1 to 6 of 6
Windows Thread, Active Directory Script in Technical; Does anyone know of a way of changing the firstname and surname fields of users in active directory via CLI? ...
  1. #1
    wesleyw's Avatar
    Join Date
    Dec 2005
    Location
    Kingswinford
    Posts
    2,211
    Thank Post
    226
    Thanked 50 Times in 44 Posts
    Blog Entries
    1
    Rep Power
    30

    Active Directory Script

    Does anyone know of a way of changing the firstname and surname fields of users in active directory via CLI?


    Wes

  2. #2

    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,216
    Thank Post
    116
    Thanked 332 Times in 274 Posts
    Blog Entries
    4
    Rep Power
    115
    Using VBScript would do it.

    Welcome to the Script Center is a good place to start.

  3. Thanks to jamesb from:

    wesleyw (26th February 2009)

  4. #3
    wesleyw's Avatar
    Join Date
    Dec 2005
    Location
    Kingswinford
    Posts
    2,211
    Thank Post
    226
    Thanked 50 Times in 44 Posts
    Blog Entries
    1
    Rep Power
    30
    Okay then I've got this VBS script:

    Const ADS_PROPERTY_UPDATE = 2
    Set objUser = GetObject _
    ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com")

    objUser.Put "givenName", "Ken"
    objUser.Put "initials", "E."
    objUser.Put "sn", "Myer"
    objUser.Put "displayName", "Myer, Ken"
    objUser.Put "physicalDeliveryOfficeName", "Room 4358"
    objUser.Put "telephoneNumber", "(425) 555-1211"
    objUser.Put "mail", "myerken@fabrikam.com"
    objUser.Put "wWWHomePage", "http://www.fabrikam.com"
    objUser.PutEx ADS_PROPERTY_UPDATE, _
    "description", Array("Management staff")
    objUser.PutEx ADS_PROPERTY_UPDATE, _
    "otherTelephone", Array("(800) 555-1212", "(425) 555-1213")
    objUser.PutEx ADS_PROPERTY_UPDATE, _
    "url", Array("http://www.fabrikam.com/management")
    objUser.SetInfo


    Now how could I modifiy this to input all of my students from active directory and parse the username to create the First Name and Surname I.E. 08Bloggs-F

    GivenName = F
    Surname = Bloggs?


    Wes

  5. #4

    Join Date
    Mar 2008
    Location
    Surrey
    Posts
    2,216
    Thank Post
    116
    Thanked 332 Times in 274 Posts
    Blog Entries
    4
    Rep Power
    115
    I'm assuming that you want to do this from a CSV file?

    If so this script may help you.

  6. #5

    Join Date
    Nov 2007
    Location
    Rotherham
    Posts
    1,678
    Thank Post
    122
    Thanked 126 Times in 102 Posts
    Rep Power
    46
    built in Windows 2003 server command, dsmod!

    Plus some hevpfullness via excel to create the script itself.

  7. Thanks to Stuart_C from:

    wesleyw (26th February 2009)

  8. #6

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,157
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    Script below should work - comment out the "oUser.setinfo" bit the first time you run it and it will just create a log showing what it's going to do. If that looks OK then re-rerun with with the setinfo line included.

    Don't have an AD handy to try this on but it's based on various scripts I've used which do "something" to every user so I'm reasonably confident :-)

    Code:
    Set oRootDSE = GetObject("LDAP://RootDSE")
    sDomain = oRootDSE.Get("defaultNamingContext")
    
    Set oFSO=wscript.createobject("scripting.filesystemobject")
    sLogPath = "c:\temp\change" & right(year(date),2) & right("00" & month(date),2) & right("00" & day(date),2) & ".log"
    Set sLogFile = oFSO.opentextfile(sLogPath, 2, True) '2 is writing
    sLogFile.Close
    WriteLog "Started"
    
    Set oConn = CreateObject("ADODB.Connection")
    oConn.Provider = "ADsDSOObject"
    oConn.Open
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConn
    oCommand.properties("Page Size")=100
    
    ocommand.CommandText = "<LDAP://" & sRoot & ">;(objectCategory=person);sAMAccountName;subTree"
    Set oRS = oCommand.Execute
    iCount=1
    Do While Not oRS.EOF
        sUsername=trim(oRS("sAMAccountName"))
        sDN=oRS("distinguishedname")
        set oUser=getobject("LDAP://" & sDN)
        wscript.echo sUsername
        iDashPos=instr(sUsername,"-")
        if iDashPos<>0 then
          sSurname=mid(sUsername,3,iDashPos-1)
          sGivenName=mid(sUsername,iDashPos+1)
          oUser.sn=sSurname
          oUser.givenname=sGivenName
          writelog "Updating " & sUsername to " & sGivenName & " " & sSurname
          oUser.setinfo
        else
          writelog "Can't find a dash in " & sUsername
        end if
        oRS.MoveNext
    Loop
    
    writelog "Ended"
    
    Sub WriteLog(sToWrite)
        Set sLogFile = oFSO.opentextfile(sLogPath, 8) '8 is append
        sLogFile.write Date & " " & Time & ": " & sToWrite & vbCrLf
        sLogFile.Close
    End Sub

SHARE:
+ Post New Thread

Similar Threads

  1. Active Directory-Script for Creating Bulk Users
    By calapso in forum How do you do....it?
    Replies: 8
    Last Post: 14th January 2009, 06:32 PM
  2. Active Directory
    By Neville in forum Windows
    Replies: 6
    Last Post: 25th June 2008, 03:24 PM
  3. Replies: 7
    Last Post: 31st January 2008, 12:17 PM
  4. script for active directory
    By chalkwellstu in forum Scripts
    Replies: 8
    Last Post: 24th September 2007, 01:53 PM
  5. Script to set active directory object security
    By meastaugh1 in forum Windows
    Replies: 4
    Last Post: 5th January 2007, 04:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •