'################################################################################ '# NAME: migrate_contacts.vbs # '# # '# This script will convert an IPSwitch IMail 2006.x (or higher) Addressbook # '# to Alt-N MDaemon 11.x AddressBook (probably also other MDaemon versions) # '# The script queries IMail's WorkgroupShare database (MS Access) for all # '# domains which are in the registry under the IMail domains key. It then # '# creates a MDaemon's AddrBook.mrk file in the MDaemon Users directory. # '# Change the 3 variables below according to your needs. # '# Use this script at your own risk! # '# # '# Author: Roel Broersma Date: 25 december 2010 Version: 1.1 # '# Company: Gigaweb B.V. E-Mail: roel@gigaweb.nl # '################################################################################ const HKEY_LOCAL_MACHINE = &H80000002 MDaemon_Folder = "X:\MDaemon\" '##Map a drive to the MDaemon directory (share), so the ## IMail_DB_path = "E:\Messaging\WorkgroupShare\Data\WorkgroupShare.mdb" '##Path to the IMail WorkGroupShare database.## imail_reg_path = "SOFTWARE\Wow6432Node\Ipswitch\IMail" '##Registry Path of IMail, for 32bit systems this would be: "SOFTWARE\Ipswitch\IMail" .## set obj_reg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&IMail_DB_path&";" set conn = CreateObject("ADODB.connection") conn.open strConn set rs = CreateObject("ADODB.Recordset") Set obj_filesys = CreateObject ("Scripting.FileSystemObject") Function AddZero(int_number) If (int_number<10) Then AddZero="0"&int_number Else AddZero=int_number End If End Function previous_UserID = "-1" obj_reg.EnumKey HKEY_LOCAL_MACHINE, imail_reg_path&"\Domains", array_domains For Each MailDomain In array_domains obj_reg.GetStringValue HKEY_LOCAL_MACHINE,imail_registry_domain_path&"\"&domain, "TopDir", domain_topdir If ( Left(LCase(domain),8)<>"$virtual" AND IsNull(domain_topdir)=False ) Then '#Note: Imail 2006 doesn't remember a separate MiddleName '#Note2: Because we do SQL on MS-Access with multiple Left-Join, we need to use parenthesis (All the (((((( and ))))) ). sql_str = "SELECT " &_ "Users.ID, Users.LoginName," &_ "Contacts.ID, Contacts.Modified, Contacts.Name, Contacts.FileAs, Contacts.JobTitle, Contacts.Company, Contacts.Department, Contacts.FirstName, Contacts.LastName, "&_ "HomeAddress.Address1, HomeAddress.Town, HomeAddress.Postcode, HomeAddress.County, HomeAddress.Country, " &_ "BusinessAddress.Address1, BusinessAddress.Town, BusinessAddress.Postcode, BusinessAddress.County, BusinessAddress.Country, " &_ "Address, " &_ "HomePhone.Number, " &_ "BusinessPhone.Number, " &_ "BusinessFax.Number, " &_ "MobilePhone.Number " &_ "FROM " &_ "((((((((Users) LEFT JOIN Contacts ON Users.ID=Contacts.Owner) " &_ "LEFT JOIN Addresses AS HomeAddress ON (Contacts.ID=HomeAddress.OwnerID AND HomeAddress.Name='Home-')) " &_ "LEFT JOIN Addresses AS BusinessAddress ON (Contacts.ID=BusinessAddress.OwnerID AND BusinessAddress.Name='Business-')) " &_ "LEFT JOIN EmailAddresses ON (Contacts.ID=EmailAddresses.OwnerID AND EmailAddresses.Name='Email-1')) " &_ "LEFT JOIN PhoneNumbers AS HomePhone ON (Contacts.ID=HomePhone.OwnerID AND HomePhone.Name='Home-Phone')) " &_ "LEFT JOIN PhoneNumbers AS BusinessPhone ON (Contacts.ID=BusinessPhone.OwnerID AND BusinessPhone.Name='Business-Phone')) " &_ "LEFT JOIN PhoneNumbers AS BusinessFax ON (Contacts.ID=BusinessFax.OwnerID AND BusinessFax.Name='Business-Fax')) " &_ "LEFT JOIN PhoneNumbers AS MobilePhone ON (Contacts.ID=MobilePhone.OwnerID AND MobilePhone.Name='Mobile-Phone') " &_ "WHERE Users.LoginName LIKE 'roel@" &MailDomain & "' " &_ "ORDER BY Users.ID ASC" rs.open sql_str, conn, 1, 1 While Not rs.Eof '#Close and write the previous AddressBook file (because a new users addressbook file will be made now..) If (rs("Users.ID")<>previous_UserID AND previous_UserID<>-1 AND addressbookfile_open=1) Then AddrBookFile.WriteLine("") AddrBookFile.Close Set AddrBookFile = Nothing addressbookfile_open = 0 End If If ( rs("Users.ID")<>previous_UserID AND Not IsNull(rs("Contacts.ID")) ) Then addressbookfile_open = 1 LoginName = Mid( rs("LoginName"), 1, InStr(rs("LoginName"),"@")-1 ) Contacts_Folder = MDaemon_Folder & "Users\" & MailDomain & "\" & LoginName &"\Contacts.IMAP" '#Create Contacts Folder if it does not exist already. If ( obj_filesys.FolderExists(Contacts_Folder)=False ) Then obj_filesys.CreateFolder(Contacts_Folder) End If '#Create HIWATER.MRK file inside Contacts folder if not exist already. If ( obj_filesys.FileExists(Contacts_Folder&"\HIWATER.MRK")=False ) Then Set IMAPFile = obj_filesys.CreateTextFile(Contacts_Folder&"\HIWATER.MRK",True,False) IMAPFile.WriteLine("[Groupware]") IMAPFile.WriteLine("FolderClass=IPF.Contact") IMapFile.Close Set IMAPFile = Nothing End If '#Create AddressBook. Set AddrBookFile = obj_filesys.CreateTextFile(Contacts_Folder&"\AddrBook.mrk",True,False) AddrBookFile.WriteLine("") End If If Not IsNull(rs("Contacts.ID")) Then Set Guid_obj = createobject("Scriptlet.TypeLib") AddrBookFile.WriteLine("") AddrBookFile.WriteLine("") AddrBookFile.WriteLine("" & DatePart("yyyy",rs("Modified"))&"-"&AddZero(DatePart("m",rs("Modified")))&"-"&AddZero(DatePart("d",rs("Modified"))) & " " & AddZero(DatePart("h",rs("Modified")))&":"&AddZero(DatePart("n",rs("Modified")))&":"&AddZero(DatePart("s",rs("Modified"))) & "") If (rs("FirstName")<>"") Then AddrBookFile.WriteLine("") End If If (rs("LastName")<>"") Then AddrBookFile.WriteLine("") End If If (rs("Name")<>"") Then AddrBookFile.WriteLine("") End If If (rs("Address")<>"") Then AddrBookFile.WriteLine("") End If If (rs("Company")<>"") Then AddrBookFile.WriteLine("") End If If (rs("JobTitle")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessPhone.Number")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessFax.Number")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomePhone.Number")<>"") Then AddrBookFile.WriteLine("") End If If (rs("MobilePhone.Number")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomeAddress.Address1")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomeAddress.Town")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomeAddress.County")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomeAddress.Postcode")<>"") Then AddrBookFile.WriteLine("") End If If (rs("HomeAddress.Country")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessAddress.Address1")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessAddress.Town")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessAddress.County")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessAddress.Postcode")<>"") Then AddrBookFile.WriteLine("") End If If (rs("BusinessAddress.Country")<>"") Then AddrBookFile.WriteLine("") End If If (rs("Department")<>"") Then AddrBookFile.WriteLine("") End If AddrBookFile.WriteLine("") addressbookfile_open = 1 Set Guid_obj = Nothing End If previous_UserID = rs("Users.ID") rs.MoveNext Wend '#Close and write the previous AddressBook file (because it's the last user at the end of the loop) If (addressbookfile_open=1) Then AddrBookFile.WriteLine("") AddrBookFile.Close Set AddrBookFile = Nothing addressbookfile_open = 0 End If End If Next Set obj_filesys = Nothing Set rs = Nothing conn.Close set conn = Nothing set obj_reg = Nothing