Transfer outlook accounts using scripting

Hi All. This blog is another in an ongoing recent theme I’ve been using; Customer Requests!

This time, I had a customer running an Office 365 hosted email solution who was looking to change their default email domain.

The user’s would still get emails sent to the current domain but the customer wanted all new emails to be sent and received from the new domain. In this blog I will show you how to change outlook accounts using scripting.

The Requirements

The back-end side of things was a simple case of adding the new domain as a secondary domain, adding it as an alias to each email account and deciding on a switchover date.

For the front-end, users would need to change their email account addresses to use the new domain. With enough documentation this wouldn’t be too big of a task, but to both co-ordinate this with a certain date to make the back-end changes, and to ensure this all runs smoothly, presented it’s own challenges.

To minimise the work required by the end users and to make co-ordinating these changes easier, we decide to write a script for the task.

The Script

After some further digging, we found that the account information stored for Outlook is not accessible (or manipulatable – is that a word?) through a normal plist file. However, Outlook does come with it’s own AppleScript library of commands or scripting elements.

Simply launch Apple’s Script Editor application and use the “+” (‘plus’) symbol in the ‘Library’ window to point to your Outlook installation in order to view and access this information.

AppleScript Library short

This library allowed me to write the following AppleScript (a copy of the script can be found on our GitHub here):

Script clean

What does the script do?

The AppleScript will check the first 3 Exchange-type accounts in Outlook, and check for the “BADDOMAIN.COM” domain in the email address and / or the username. If found, the script will replace “BADDOMAIN.COM” with “GOODDOMAIN.COM”.

This would effectively turn a username and / or email address from ‘bob@baddomain.com’ to ‘bob@gooddomain.com’.

How do I actually use this?

Firstly, you’ll need to get the script setup for your environment. You’ll need to use a ‘find and replace’ on “BADDOMAIN.COM” to set this to the domain you are looking to swap out.

Next, you’ll have to do another ‘find and replace’ on “GOODDOMAIN.COM” to set this to the domain you are looking to swap in.

To actually trigger / run the script, you have a few options:

  1. Save the script as an AppleScript Application
    1. This will allow users to double-click it and run it themselves.
  2. Trigger the script with a Login Item or Launch Daemon.
    1. This will automate the running of the script for the logging in user.
  3. Automate the running of the script by your Mac management solution.
    1. However, as most management solutions run their scripts as root, you’ll need to do some work!

Convert the AppleScript to allow it to be run by a management solution

Full Disclosure: The client uses the JAMF Casper suite to manage their client devices, so the next piece of work has been written with that in mind.

The script should also work with Munki but will need further checks added and tests run to ensure that a user is actually logged into the Mac when it runs. For Casper, this is managed with the use of policy triggers (specifically using the “Login” trigger).

The simplest method I found, was to wrap the AppleScript in a bash script, and use the bash script to run the contained AppleScript as the currently logged in user (obtained from Ben Tom’s method here).

But how? Here’s an example on our GitHub, which I used with the above linked AppleScript, wrapped in a bash script.

What if the user has multiple copies of Outlook installed?

If the user has multiple copies of Outlook installed (e.g. Outlook 2011 and Outlook 2016) the script will run on Outlook 2016 accounts only.

However, I have performed some basic testing with Outlook 2011 and the script will work. If you need to target a specific version of Outlook (2016 or 2011), change the “tell Application Outlook” line of each script to use the full path to the version of Outlook you want to target (e.g. “/Applications/Microsoft Outlook.app” for 2016 and “/Applications/Microsoft Office 2011/Microsoft Outlook.app” for 2011). The lines in question are:

  • For the AppleScript, line 25
  • For the Bash script, line 34

Looking forward…

Although the script is now ‘complete enough’ for my usage, there’s a few bits and pieces I’d like to fix / change with it:

  1. Switch to using a proper loop through the exchange accounts, rather than the manual 3 account loop I’ve built currently
  2. Turn the “GOODDOMAIN.COM” and “BADDOMAIN.COM” placeholders into true variables to make the script simpler to use
  3. Add a ton more comments to the whole thing, to make it easier to read and understand.

Hopefully I’ll get time to work on these, but please do feel free to take my barebones script and modify it to suit your needs.

I had a conversation with Mario (‘@mario’) on the MacAdmins Slack group who used an adapted version of this script to modify the LDAP Server value!

Summary

There you go, hopefully that’ll give other Mac Admins a helping hand when trying to script changes to Outlook accounts for their customers. As always, if you have any questions, queries or comments, let us know below and I’ll try to respond to and delve into as many as I can.

The usual Disclaimer:

While the author has taken care to provide our readers with accurate information, please use your discretion before acting upon information based on the blog post. Amsys will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.

 

Leave a Reply

Your email address will not be published. Required fields are marked *