📜 ⬆️ ⬇️

Importing contacts into Opera from Active Directory

I work in a company in which the number of employees having an email tends to infinity. Of course, all these people are in AD with first and last names. And once again, when I climbed up to look at the employee’s email address on the corporate portal, I was bored and I thought about importing contacts into Opera from AD.

Search in Yandex goggles did not give me anything, so I decided to engage in pioneering. To begin with, I looked into the file contacts.adr, which is stored in the profile folder and I realized that it is stored in UTF-8 encoding and has a certain structure:

  1. #CONTACT
  2. ID = 1
  3. NAME = Ivanov Ivan Ivanovich
  4. URL =
  5. CREATED = 1287730972
  6. DESCRIPTION =
  7. MAIL = ivanov @ domain. com
  8. PHONE =
  9. FAX =
  10. POSTALADDRESS =
  11. PICTUREURL =
  12. ICON = Contact0
  13. #CONTACT
  14. ID = 2
  15. NAME = Petrov Petr Petrovich
  16. URL =
  17. CREATED = 1292317914
  18. DESCRIPTION =
  19. MAIL = petrov @ domain. com
  20. PHONE =
  21. FAX =
  22. POSTALADDRESS =
  23. PICTUREURL =
  24. ICON = Contact0


ID, NAME, MAIL, etc. questions do not cause. The CREATED field seems to store the date and time of the contact creation, but for me it is not important, and therefore I decided to write down a specific value taken from the finished file.
')
It is time to start creating the import tool directly. Having searched the network for examples of working with AD, I wrote this:

  1. var rootDSE , domainNC , connection , command , recordset ;
  2. var fso , f1 ;
  3. rootDSE = GetObject ( "LDAP: // rootDSE" ) ;
  4. domainNC = rootDSE. Get ( "defaultNamingContext" ) ;
  5. connection = new ActiveXObject ( "ADODB.Connection" ) ;
  6. connection. Open ( "Provider = ADsDSOObject;" ) ;
  7. command = new ActiveXObject ( "ADODB.Command" ) ;
  8. command. ActiveConnection = connection ;
  9. command. CommandText =
  10. "SELECT displayname, mail, telephoneNumber FROM 'LDAP: //" + domainNC + "' WHERE objectClass = 'User'"
  11. command. Properties ( "Cache Results" ) = false ;
  12. command. Properties ( "Page Size" ) = 100 ;
  13. command. Properties ( "Sort On" ) = "mail" ;
  14. command. Properties ( "Timeout" ) = 0 ;
  15. recordset = command. Execute ( ) ;
  16. fso = new ActiveXObject ( "Scripting.FileSystemObject" ) ;
  17. f1 = fso. CreateTextFile ( "contacts.adr" , true ) ;
  18. i = 1 ;
  19. while ( ! recordset. EOF ) {
  20. if ( ( recordset. Fields ( "mail" ) . Value ! = null ) && ( recordset. Fields ( "displayname" ) . Value ! = null ) )
  21. {
  22. f1. WriteLine ( "#CONTACT" ) ;
  23. f1. WriteLine ( "ID =" + i ) ;
  24. f1. WriteLine ( "NAME =" + recordset. Fields ( "displayname" ) . Value ) ;
  25. f1. WriteLine ( "URL =" ) ;
  26. f1. WriteLine ( "CREATED = 1292317914" ) ;
  27. f1. WriteLine ( "DESCRIPTION =" ) ;
  28. f1. WriteLine ( "MAIL =" + recordset. Fields ( "mail" ) . Value ) ;
  29. f1. WriteLine ( "PHONE =" + recordset. Fields ( "telephoneNumber" ) . Value ) ;
  30. f1. WriteLine ( "FAX =" ) ;
  31. f1. WriteLine ( "POSTALADDRESS =" ) ;
  32. f1. WriteLine ( "PICTUREURL =" ) ;
  33. f1. WriteLine ( "ICON = Contact0" ) ;
  34. }
  35. recordset. MoveNext ( ) ;
  36. i = i + 1 ;
  37. }
  38. f1. Close ( ) ;
  39. connection. Close ( ) ;

Simply put, we make a request to the domain and take out the name, mail and phone number of the employee. Then we sort everything by e-mail address and, cutting off records with empty addresses or empty names (I for some reason had such), write all this to a file, constantly increasing the contact ID. ID, perhaps, it makes sense to start not with one, but with a larger number, because This will allow you to leave existing contacts in your address book.
I saved this ... ahem ... code in the ad.js file and launched cscript ad.js from the console.

Having received the coveted file, I changed the encoding for it using notepad ++ to UTF8 and replaced the file in the profile folder when Opera was closed.
As a result, I received several hundred contacts in my address book with a flick of the wrist :)

Also, if a department in which an employee works is stored in AD, you can get confused and scatter contacts into folders using the #FOLDER “tag”. An inquisitive reader, if desired, can himself figure out how to do it :)

Of course, probably, there is an easier and more elegant way to do the same, but my bike, created in 10 minutes, took me exactly where I needed it.

Source: https://habr.com/ru/post/118742/


All Articles