Altering existing Outlook account setup

AdamH
New Contributor II

So we are in the process of migrating to Outlook 365.
Previously, we have used the infamous Talking Moose method of setting up Outlook for our users.
This is easily adaptable to use for O365, but existing users may pose a problem.

I'm looking for a clean way to help users change the User Name field in their Outlook account settings.
Currently its just their basic username from LDAP, however for O365 to work properly, that User Name field should be populated with their email address. Either changing the field name or something that deletes the current account and adds a new account pre-configured with the right settings would be good- but creating new ApplScripts are not my forte.
Any help or direction would be great.

12 REPLIES 12

mm2270
Legendary Contributor III

You could use the example, yet again from @talkingmoose, on this thread from 2014: https://www.jamf.com/jamf-nation/discussions/11470/outlook-2011-under-accounts-automating-changing-a...

Using his example, something like so should work:

-- get the currently logged in user's short name

tell application "System Events"
    set UserName to name of (current user)
end tell

-- read the currently logged in user's email address from Active Directory
-- be sure to update the NETBIOS domain name in the next line

set ADLookup to "dscl "/Active Directory/DOMAIN/All Domains" read /Users/" & UserName & " EMailAddress | awk '{print $2}'" as string
set EmailAddress to do shell script ADLookup


tell application "Microsoft Outlook"
    try
        set full name of exchange account 1 to EmailAddress
    end try
end tell

mm2270
Legendary Contributor III

Ah, sorry. I see now that you said you needed to change the user name to the email address. For some reason I read that as the full name, not sure why. To change the user name to the email address might actually be easier. Assuming for a moment that the users' accounts already have their correct email address in the E-mail address field in Outlook, this script should do the trick

tell application "Microsoft Outlook"
    try
        set MyEmail to (get email address of exchange account 1)
        set user name of exchange account 1 to MyEmail
    end try
end tell

Basically this tells Outlook to look at the Exchange account, read the current email address, store it as a variable and then flip the user name to be the same as that variable.

I don't know if you're working with Exchange accounts, but I'm assuming so. If not, you might need to finagle this script to work with whatever accounts your users have.

AdamH
New Contributor II

We are working with Exchange currently... migrating to O365.
This script does the trick very nicely, many thanks!

Now the question is to just make it a manual Self Service thing or try to automate it.
Maybe if I have it check to see if the username is = to the email address already, have it quit without doing anything.

mm2270
Legendary Contributor III

Glad that helped.

I would probably make it a Self Service policy. The reason is, for this script to work, or any Applescript that manipulates Outlook really, it requires having Outlook running. That also means someone needs to be logged in of course. If this ran on Macs that were sitting at the login screen, it would fail. If it ran on Macs that had someone logged in, but Outlook wasn't already running, it would launch Outlook, which might freak some people out since they didn't specifically open it. As such, I would allow the user's themselves to run it so they know what's going on.
You could even have an Applescript window pop up at the end to let them know the change is complete. Take a look at the thread I linked to before for the syntax @talkingmoose uses in that script as an example.

And yes, its probably a good idea to get the existing username and email address, compare them first to make sure the change even needs to happen and then do it or not.

AdamH
New Contributor II

Yeah, I was thinking along those lines as well because Outlook needs to be running.

A method I was pondering was creating a Launch agent that kicks-off the script when Outlook is launched, then checks to see if the Username is the email address, if it isn't, then makes the change. if it IS, then removed the agent and quits without making the change.
Most computers here are Single-user, so once the change is made, it shouldn't have to run again.

mm2270
Legendary Contributor III

That might work. It sounds a bit similar to the method talkingmoose employs with his Outlook set up script. Back in Office 2011, it was possible to set up a scheduled task, but things changed in Outlook 2016 so it now requires a LaunchAgent to do the same thing I believe.
The only issue I see is, how to trigger the script running? You'll need to figure out what changes or gets touched when Outlook launches. Maybe the ~/Library/Group Containers/UBF8T346G9.Office directory? I think the date changes on that each time the app is launched, so you may be able to use that as a WatchPath in the LaunchAgent.

Good luck, and post back if you need any more assistance with this.

AdamH
New Contributor II

Yeah, back in the day I used the setup script as a login script, but the launch agent approach made much more sense and didn't require Casper to run.

So I've created a launch agent that is very similar to the new 'Moose' one. Works great so far but the only sticking point is getting the script to quit if the username is already the email address, or if its ANY email address, such as contains @.
Probably easily done for any AppleScript gurus out there, but I don't seem to be getting the syntax right.

AdamH
New Contributor II

OK, silly me forgot the ELSE.....

I put in a delay at the beginning because it was being interrupted by the actual Outlook login
So here's what I put together:

delay 30

tell application "Microsoft Outlook" try set MyEmail to (get email address of exchange account 1)

if user name of exchange account 1 = MyEmail then error number -128

else

set user name of exchange account 1 to MyEmail

end if end try

end tell

Thanks for the help!

Ken_Bailey
New Contributor III

Hello,

Was wondering if anybody ran into this. I am using.

tell application "Microsoft Outlook" try set MyEmail to (get email address of exchange account 1) set user name of exchange account 1 to MyEmail end try end tell

However it is populating the user name with domainemailaddress vs just email address.

Any help greatly appreciated.

haiko_solakian
New Contributor

Hi Ken,

you can easily set the domain to blank:

tell application "Microsoft Outlook"
    try
        set MyEmail to (get email address of exchange account 1)
        set user name of exchange account 1 to MyEmail
        set domain of exchange account 1 to ""
    end try
end tell

Ken_Bailey
New Contributor III

Wondering if anybody had experience with users who had multiple accounts.

Had an issue where the exchange account 1 was not the wanted targeted account so the script changed the user name of the incorrect account.

daniel_behan
Contributor II

I have the following script that will compare the email address field with the user name field in outlook and update them in needed. It will also enter the results into a text file that can be read in a subsequent Extension Attribute, but I'm having issues creating a TCC profile that will allow Jamf Agent to control Outlook for machines running Mojave and Catalina. I've been trying the PPPC Utility with no success.

#!/bin/bash

User=`/usr/bin/stat /dev/console | awk '{print $5}'`

#Check to see if Outlook has been used

Outlook=`/bin/ls /Users/$User/Library/Containers/com.microsoft.Outlook | /usr/bin/grep Container.plist`
if [ $Outlook != "Container.plist" ]; then
exit 0
else

#Check to see if Outlook is running
PIDS=`ps cax | grep "Outlook" | grep -o '^[ ]*[0-9]*'`
if [ -z "$PIDS" ]; then
echo "Process not running" 1>&2
exit 0

else
sudo -u $User /usr/bin/osascript > /Users/Shared/OutlookUser.txt <<-EOF

tell application "Microsoft Outlook"
        try
                set MyEmail to (get email address of exchange account 1)
                get user name of exchange account 1
                if user name of exchange account 1 is not equal to MyEmail then
                        set domain of exchange account 1 to ""
                        set user name of exchange account 1 to MyEmail
                        set r1 to "Changed"
                else
                        set r2 to "No Change"
                end if
        end try
end tell
EOF
fi
fi
exit 0