integrating jamfHelper full screen window and applescript to migrate Entourage to Outlook 2011

We are about to migrate the company Exchange environment from 2003 to 2010. While our Windows brethren have been using Outlook 2010 for quite some time now, us Mc folk have been left in the dust to deal with Entourage 2004 and 2008 (Non-EWS) as the email client of choice. In my constant quest for full automation of absolutely everything, @talkingmoose on the JAMF Nation discussion boards helped me out with an absolutely superb script he wrote that will migrate an Entourage 2004 or 2008 identity to Outlook 2011 AND provides a method of giving the user visual feedback on the progress of the migration! You can grab all of the code and documentation here: https://github.com/talkingmoose/Outlook-Exchange-Setup/tree/4.5

The management team at my company does not want the end users intervening during the migration, however, so I’ve had to tweak the script a bit to A) not allow the users to choose which identity to migrate. Rather it detects if they have an Entourage 2004 or 2008 identity, and migrates it automatically (if they have both, it takes the newest version). And B) it utilizes the jamfHelper to display a fullscreen window to lock the desktop of the Mac while the migration completes in the background. You do lose the ability to provide users visual feedback on how long the migration has been running however.

The code I had to add is below:

set jamfHelperPID to do shell script "ps -ax | grep -v grep | grep jamfHelper | awk '{print $1}'"
if jamfHelperPID is not equal to "" then
 do shell script "killall jamfHelper"
end if
do shell script "/Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType fs -heading 'Your Email Account is being Migrated' -description 'We are now upgrading your email account from Entourage to Outlook 2011. Your upgrade may take between 5 minutes and 2 hours depending on how much email you have. If you still see this window after 2 hours, please contact the helpdesk at XXXX. Please do not turn off this computer. This message will go away when the upgrade is complete.' -icon /Applications/Microsoft\ Office\ 2011/Microsoft\ Outlook.app/Contents/Resources/Outlook.icns > /dev/null 2>&1 &"

Essentially what this does is to throw up a fullscreen window using jamfHelper to provide some useful information to the end user. The full screen also prevents them from interacting with the computer so the migration can take full advantage of processor and memory in the Mac. The important part of the code is the “> /dev/null 2>&1 &.”  This addition to the jamfHelper command directs it’s non-existent output to /dev/null, and the & causes the script to execute the rest of the commands while this one command continues to run. We have to capture each instance of the jamfHelper process and kill it if necessary, because otherwise we may end up with many, many  jamfHelper windows being spawned. Once the migration is done, we need to issue one final command to kill the jamfHelper window, but this command has to be in a separate tell block in Applescript:

tell application "System Events"
 if jamfHelperPID is not equal to "" then
  do shell script "killall jamfHelper System\ Events"
 end if
end tell

We also have to kill System Events according to JAMF, otherwise we’ll see strange output from the keyboard, and every mouse click may be interpreted as a right-click. Killing System Events prevents this. This code is being run completely in the user context, so jamfHelper DOES not to be made readable by everyone. That’s the only caveat here. Once that’s done, no escalation of privileges needs to take place, since every sh, System Events, and jamfHelper process is spawned by the user.

The entire migration loop written by talkingmoose and tweaked by me for my environment and requirements looks like this:

tell application "System Events"
 set PID to unix id of process "Microsoft Database Daemon"
 set CPUUsage to 10
 set CPUCounter to 0

 set startTime to (current date)
 repeat while CPUUsage as integer ≥ 5 or CPUCounter ≤ 3 -- The Microsoft Database Daemon must be utilizing less than 5% of CPU for one minute before the import is considered complete

 -- Adding a try statement here because sometimes the result of this command is simply garbage.

 set elapsedTime to (current date) - startTime

 -- format the time as "00:00:00"

 set hrs to elapsedTime div hours
 if hrs < 10 then
 set hrs to "0" & hrs as string
 end if
 set mins to elapsedTime div minutes
 if mins < 10 then
 set mins to "0" & mins as string
 end if
 set secs to elapsedTime mod minutes
 if secs < 10 then
 set secs to "0" & secs as string
 end if

 set formattedTime to hrs & ":" & mins & ":" & secs as string

 try
 set jamfHelperPID to do shell script "ps -ax | grep -v grep | grep jamfHelper | awk '{print $1}'"
 if jamfHelperPID is not equal to "" then
 do shell script "killall jamfHelper"
 end if
 do shell script "/Library/Application\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType fs -heading 'Your Email Account is being Migrated' -description 'We are now upgrading your email account from Entourage to Outlook 2011. Your upgrade may take between 5 minutes and 2 hours depending on how much email you have. If you still see this window after 2 hours, please contact the helpdesk at XXXX. Please do not turn off this computer. This message will go away when the upgrade is complete.' -icon /Applications/Microsoft\ Office\ 2011/Microsoft\ Outlook.app/Contents/Resources/Outlook.icns > /dev/null 2>&1 &"
 on error
 activate
 display dialog "Process too busy to read at this moment. Will try again in " & refreshRate & " seconds." giving up after refreshRate buttons {"OK"} with title "Upgrading Entourage Database…" with icon alias ((path to applications folder as string) & "Microsoft Office 2011:Microsoft Outlook.app:Contents:Resources:Outlook.icns")
 end try

 set CPUUsage to do shell script "ps aux | grep " & PID & " | grep -v grep | awk '{print $3}'"

 -- The "Microsoft Database Daemon" may be inactive for several seconds during the import process but the import may not necessarily be done. This section ensures that the daemon has been at less than 5% CPU usage 3 times in a row before considering the import is complete.

 if CPUUsage as integer ≤ 5 then
 set CPUCounter to CPUCounter + 1
 else
 set CPUCounter to 0
 end if
 end repeat
 end tell
end if

--------------------------------------------
-- End account setup
--------------------------------------------

--------------------------------------------
-- Script clean
--------------------------------------------

-- We're done. Set the schedules back to normal and set Outlook to work online

tell application "Microsoft Outlook"
 set enabled of schedule "Send & Receive All" to scheduled
 delete exchange account 2 -- upgrading an identity causes a second account to be created and we certainly don't want that
 tell application "System Events"
 if jamfHelperPID is not equal to "" then
 do shell script "killall jamfHelper System\ Events"
 end if
 --activate
 --display dialog "Outlook is ready to use." buttons {"OK"} default button {"OK"} with icon alias ((path to applications folder as string) & "Microsoft Office 2011:Microsoft Outlook.app:Contents:Resources:Outlook.icns") with title "Setup Complete"
 end tell
 set working offline to false
 delete schedule "Exchange Setup"
end tell

EDIT 9/21/2012:

Some people on other message boards have requested I post my finished script, so here is the code I used:

set homeFolder to path to home folder as string
set Office2008Folder to homeFolder & "Documents:Microsoft User Data:Office 2008 Identities"
set Office2004Folder to homeFolder & "Documents:Microsoft User Data:Office 2004 Identities"
set OfficeIdentities to 0
set FoldertoMigrate to ""

tell application "Finder"
set OfficeIdentities to {}

if (exists of folder Office2004Folder) is true then
set Office2004Identities to every folder of folder Office2004Folder whose name does not contain "Backed up" and name does not contain "copy"
repeat with identity from 1 to count of Office2004Identities
copy item identity of Office2004Identities to end of OfficeIdentities
end repeat
set FoldertoMigrate to homeFolder & "Documents:Microsoft User Data:Office 2004 Identities:Main Identity:"
end if

if (exists of folder Office2008Folder) is true then
set Office2008Identities to every folder of folder Office2008Folder whose name does not contain "Backed up" and name does not contain "copy"
repeat with identity from 1 to count of Office2008Identities
copy item identity of Office2008Identities to end of OfficeIdentities
end repeat
set FoldertoMigrate to homeFolder & "Documents:Microsoft User Data:Office 2008 Identities:Main Identity:"
end if

set choicesList to {}

end tell

--------------------------------------------
-- End check for Entourage identities to import
--------------------------------------------

--------------------------------------------
-- Begin account setup
--------------------------------------------

set upgradeResponse to "New"

if upgradeResponse is "New" then

-- Configure a brand new Outlook database

try

if verifyEMailAddress is true then
set verifyEmail to display dialog "Please verify that your email address is correct." default answer emailAddress with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set emailAddress to text returned of verifyEmail
end if

if verifyServerAddress is true then
set verifyServer to display dialog "Please verify that your Exchange Server name is correct." default answer ExchangeServer with icon 2 buttons {"Cancel", "OK"} default button {"OK"}
set ExchangeServer to text returned of verifyServer
end if

tell application "Microsoft Outlook"
activate
set newExchangeAccount to make new exchange account with properties ¬
{name:"Mailbox - " & fullName, user name:shortName, full name:fullName, email address:emailAddress, server:ExchangeServer, use ssl:ExchangeServerRequiresSSL, port:ExchangeServerSSLPort, ldap server:DirectoryServer, ldap needs authentication:DirectoryServerRequiresAuthentication, ldap use ssl:DirectoryServerRequiresSSL, ldap max entries:DirectoryServerMaximumResults, ldap search base:DirectoryServerSearchBase}

-- The following lines enable Kerberos support if the userKerberos property above is set to true.

if useKerberos is true then
set use kerberos authentication of newExchangeAccount to useKerberos
set principal of newExchangeAccount to shortName & "@" & kerberosRealm
end if

-- The Me Contact record is now automatically created with the first account.
-- Set the first and last name of the Me Contact record.

set first name of me contact to firstName
set last name of me contact to lastName
set email addresses of me contact to {address:emailAddress, type:work}

end tell

on error

display dialog errorMessage with icon 2 buttons {"OK"} default button {"OK"}
error number -128

end try
end if

if FoldertoMigrate is not equal to "" then
set upgradeResponse to "Upgrade"
end if

if upgradeResponse is "Upgrade" then

-- Import the selected Entourage database into Outlook

-- This begins the actual import. To provide the user some feedback about progress the script will not wait for this process to complete but will continue to the next section to display a pseudo-timer.

set importResult to ""
do shell script "/Library/Application\\ Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType fs -heading 'ISD is migrating your email account' -description 'We are now upgrading your email account from Entourage to Outlook 2011. Your upgrade may take between 5 minutes and 2 hours depending on how much email you have on your Mac. If you still see this window after 2 hours, please contact the helpdesk. Please do not turn off this computer. This message will go away when the upgrade is complete.' -icon /Library/Outlook\\ Setup\\ Script/AGrose.icns > /dev/null 2>&1 &"

tell application "Microsoft Outlook"
set working offline to true
with timeout of 14400 seconds
set importResult to import identity FoldertoMigrate
end timeout
end tell

end if

--------------------------------------------
-- End account setup
--------------------------------------------

--------------------------------------------
-- Script clean
--------------------------------------------

-- We're done. Set the schedules back to normal and set Outlook to work online
set jamfHelperPID to do shell script "ps -ax | grep -v grep | grep jamfHelper | awk '{print $1}'"
tell application "Microsoft Outlook"
activate
set enabled of schedule "Send & Receive All" to scheduled
delete exchange account 2 -- upgrading an identity causes a second account to be created and we certainly don't want that
tell application "System Events"

if jamfHelperPID is not equal to "" then
do shell script "killall jamfHelper System\\ Events"
end if
end tell
set working offline to false
delete schedule "Exchange Setup"
end tell

--------------------------------------------
-- End script clean
-------------------------------------------

Again, a big thank you to @talkingmoose on JamfNation.com for all his hard work developing the main script. I only made edits for our environment. There is a lot of cruft in my edit that I haven’t had time to go back and clean up.  I estimate the migration speed at about 1GB per 4 minutes, depending on HDD speed of course. Enjoy!


About this entry