+1 vote

I need to use Mail.dll IMAP client to download emails from Office365 using OAuth.

When registering service principals in Exchange on step:

Create service principal

New-ServicePrincipal -AppId <APPLICATION_ID> -ServiceId <OBJECT_ID> [-Organization <ORGANIZATION_ID>]

I got an error:

New-ServicePrincipal: The term 'New-ServicePrincipal' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that path is correct and try again.

Do you need to install anything else to make this work?

by (780 points)

Never mind my question, I haven't installed the azure power shell on my machine.

1 Answer

0 votes
Best answer

As described here:

You need to set execution policy:

Set-ExecutionPolicy RemoteSigned 

and install ExchangeOnlineManagement module:

Install-Module -Name ExchangeOnlineManagement
by (298k points)

From which package should New-ServicePrincipal belong? I can't find it from Get-Command -ListImported command in Powershell.

It's a simple step-by-step instruction:

Install module:

Install-Module -Name ExchangeOnlineManagement


Connect-ExchangeOnline -UserPrincipalName your-admin-account@your-domain.onmicrosoft.com

Create service principal

New-ServicePrincipal -AppId <APPLICATION_ID> -ServiceId <OBJECT_ID> [-Organization <ORGANIZATION_ID>]

Assign permission:

Add-MailboxPermission -Identity "AdeleV@your-domain.onmicrosoft.com" -User <OBJECT_ID> -AccessRights FullAccess

If you still get an error running the New-ServicePrincipal Cmdlet after you perform these steps, it is likely due to the fact that the user doesn't have enough permissions in Exchange online to perform the operation.

I'm at the step
Connect-ExchangeOnline -UserPrincipalName user@domain.onmicrosoft.com
which then prompts me to login to the exchange account but user@domain.onmicrosoft.com doesn't work for logging in, and when I try user@domain.com it logs in but powershell says Admin account chosen for authentication is different from the one provided as parameter during Connect-ExchangeOnline. Please help!!

The only advice I can give, is to use the same email address in both places.