Importing Personal Contact CSV exports to Users’ mailboxes

The Challenge: 

You’re migrating a client’s email from a hosted, web-only service to Office 365 – more specifically, Exchange Online (EO). The users all have contacts in the web interface and exporting them is possible, but only in .CSV format. How do you get all of these contacts into the users’ new EO mailboxes?

My Solution:

My initial reaction was to send the users instructions on how to do this themselves, as it’s not very hard. Depending on your client or user base, this may not be an option. (It wasn’t for me.)

Another option if you’re only dealing with a handful of mailboxes: Import the .CSV into an Outlook profile, then export it to .PST. From there, you have multiple options for importing them. I had ~150 mailboxes to do this for, so this method would have taken me a long, long time.

First you need to export each of the user’s contacts to .CSV. Hopefully, you can batch or script this – I wasn’t so lucky, so I spent a lot of time clicking.

I saved each of the files in a folder called “C:\CONTACTEXPORTS” and named each of the files “first.last.csv.”

Then I found a blog post from a guy named Steve Goodman, who wrote a script that imports a .CSV into a user’s mailbox using EWS. I downloaded his script and followed his instructions – actually, I added the “ApplicationImpersonation” role to my import user via the EO Exchange Admin Console (EAC) like this:

  1. In the Office 365 admin center, choose the ADMIN in the left pane, and then choose Exchange.
  2. In the Exchange admin center, choose permissions > admin roles, and then select Compliance Management.
  3. Choose the Plus symbol (+) to add a new role group.
  4. In the Role Group dialog box, type a name and a description for the role group. (I used “CONTACT IMPORT”)
  5. To add a role, choose the Plus symbol (+) above the Roles list.
  6. In the Select a Role dialog box, choose ApplicationImpersonation in the DISPLAY NAME list, and then choose add > OK.
  7. In the new role group dialog box, choose the Plus symbol (+) above the Members list, and then select the users you want to have this role.

But when I tried to run his script against my EO environment I got this error:

Exception calling “AutodiscoverUrl” with “1” argument(s): “Autodiscover blocked a potentially insecure redirection tohttps://autodiscover-s.outlook.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload.”
At line:1 char:25
+ $service.AutodiscoverUrl <<<< (“hkrause@infinitec.de”);
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

I’m not by any means a programmer, so back to Google I went and I was lead to this blog post by another guy named Henning Krause.

The fix is to simply edit line 160 of Steve’s script from this:

$service.AutodiscoverUrl($EmailAddress);

To this:

$service.AutodiscoverUrl($EmailAddress, {$true});

Now… this worked great for testing the script on a single mailbox. (PROTIP: Don’t try this the first time on the CEO’s mailbox. Screw up someplace safer first.)

Finally we get to the point where I feel like I contributed something. I created a powershell script that maps all the ‘contact’ .CSVs to their new EO mailboxes. It uses another .CSV to do that mapping (named “ContactsImport.csv”), formatted like this:

Screenshot_2_25_16__8_32_PM

Then the powershell command is:

Import-CSV “C:\CONTACTEXPORTS\ContactsImport.csv” |%{.\Import-MailboxContacts.ps1 -CSVFileName $_.Contacts –EmailAddress $_.EmailAddress -Impersonate $true -Username impersonator@contoso.com -Password password}

One last thing – make sure the script and all .CSV files are in the same folder (in this case, “C:\CONTACTEXPORTS”) because the script and .CSV files use relative paths.

I hope Steve, Henning, and I have helped you out! If you’ve solved this same challenge in another way, I’d love to know about it. I’m on Twitter @BillGaleckas!

So what’s the point?

Basically, I have no idea. I just thought that I could offer some help, advice, clarity, or real-world examples on several things that a lot of SMB system and network administrators run into.

I was going to run this blog off of a Raspberry Pi, but instead I decided to use the first one as a home network ad-blocker and the other one as a retro video game system.

As you can see from the image above and the WordPress template, I have no eye for design or creative bones in my body. It’s a character flaw, but one I don’t think I have the energy to try and change. I’ll try to work around it as best I can.