|
In environments that exists for several years you have most of the time different naming conventions. Sometimes a manager get's a 'brilliant' idea to set the naming convention totally different from what it was. Strugling with this you have to rename all the users again to be complient to the company's naming convention, this can be a pain in the bud but thanks to little scripts found on the internet life can made easier.
I have written a script with the help of Elevator from GOT a dutch website were you can ask questions or search for stuf. The script i've written sets the DisplayName as Name. If you open active directory users and computers in the right pane you get the 'Name' this attribute needs a move command before it can be renamed. Before you begin you have to set the DisplayName right. This is done by a script I found on David Wiseman his website. Not all names are renamed the right way so check for names with for example last name: John From Nothingham. This will be set as From Nothingham, John. You have to manually rename the DisplayName. 'script begin set cn = createobject("ADODB.Connection") set cmd = createobject("ADODB.Command") set rs = createobject("ADODB.Recordset") cn.open "Provider=ADsDSOObject;" cmd.activeconnection = cn cmd.properties("page size")=500 set objRoot = getobject("LDAP://RootDSE") cmd.commandtext = "SELECT adsPath,givenName,sn FROM 'LDAP://ou=SSC,ou=citrix,dc=osiris,dc=local'" & _ " WHERE objectCategory = 'User' and givenName='*' and sn ='*'" set rs=cmd.execute while rs.eof <> true and rs.bof <> true '<<<< Store new displayName in variable >>>> newDisplayName = rs("sn") & ", " & rs("givenName") '<<<< Bind to user object >>>> set objUser = getobject(rs("adspath")) objUser.put "displayName", newDisplayName objUser.setinfo set objUser = Nothing rs.movenext wend cn.close wscript.echo "Finished" 'script end Well here begins the script I've written that will set the Name attribute right. Copy paste the script into notepad and name it what ever you want with the extension .vbs 'Script begin 'Script written for www.datacrash.net by Hans Straat with help of Elevator of http://gathering.tweakers.net 'This script is AS IS and for your own risk to use! 'At VarOU set the OU you want to target to rename the names. DIM objUser, oConnection, oCommand DIM RS, strQuery 'specify logfile for error handling LogFileName = "" LogFileName = replace( LogFileName, ":", "-" ) LogFileName = LogFileName & "Rename_users.TXT" Set LogFile = CreateObject("Scripting.FileSystemObject") Set oLogFile = LogFile.CreateTextFile( LogFileName, True) Set oConnection = CreateObject("ADODB.Connection") oConnection.Open "Provider=ADsDSOobject;" 'Set the path to the OU were the users reside in e.g. OU=users,dc=domaincontroller,dc=extension (.com) varOU = "ou=users,dc=test,dc=local" strQuery = "LDAP://" & varOU set oCommand = CreateObject("ADODB.Command") oCommand.ActiveConnection = oConnection oCommand.CommandText = "SELECT * FROM '" & strQuery & "'" oCommand.Properties("SearchScope") = 1 ' ADS_SCOPE_SUBTREE 'echo the qeury path wScript.echo oCommand.CommandText Set RS = oCommand.Execute wscript.echo RS.RecordCount & " users found" If RS.RecordCount = 0 Then wscript.echo "There are no users" Else While Not RS.EOF Set objUser = GetObject(RS.Fields("adspath")) wscript.echo "Alias: " & objUser.name ' If surname or givenname (e.g Smit or John) or empty write to logfile the text "cannot process" and username If( objUser.sn = "" OR objUser.givenName ="") Then oLogFile.WriteLine(" Cannot Process " & objUser.name & "" ) else 'if surname or givenname (e.g Smit John) contains a @ write to logfile "cannot process" and username If( (Instr(objUser.sn, "@")>0) or (Instr(objUser.GivenName, "@")>0) ) then oLogFile.WriteLine(" Cannot process" & objUser.name & "") else 'get objects to set strdisplayname to current DisplayName strDisplayName = (objUser.DisplayName) 'set strDisplayname to """"smit, John"""" strNewName = """"& strDisPlayName & """" 'get object Name and set strname to Name strName = (objUser.cn) 'strCurrentName = strName (e.g) """"smit john"""" strCurrentName = """"& strName & """" set objCont = GetObject("LDAP://" & varOU) ologfile.writeline ("ldap://cn" & strNewName & "," & varOU & " " & "renamed to cn" & strNewName) objCont.MoveHere "LDAP://cn=" & strCurrentName & "," & varOU, "cn=" & strNewName set objUser = GetObject("LDAP://cn=" & strNewName & "," & varOU) objUser.SetInfo 'write name object that is renamed to DisplayName object to logfile oLogFile.WriteLine("users processed" & strNewName & "") end if end if rs.movenext wend end if 'Script end |