PowerShell EWS AutoDiscoverURL Error

Jairo Javier Baleta Cali 131 Reputation points
2023-05-16T21:25:44.0633333+00:00

Good afternoon.

I hope you are well.

I am looking for the option to access an Outlook mailbox (Exchange) through a token without having to enter the credentials of a user. With the following code:

$TenantId = “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”
$AppClientId = “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”
$ClientSecret = “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”

$RequestBody = @{client_id=$AppClientId;client_secret=$ClientSecret;grant_type=”client_credentials”;scope=”https://graph.microsoft.com/.default”;}
$OAuthResponse = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token -Body $RequestBody
$AccessToken = $OAuthResponse.access_token

Import-Module "C:\Program Files\Microsoft\Exchange\Web Services2.2\Microsoft.Exchange.WebServices.dll"

$MailboxName ="usuario@dominio.onmicrosoft.com"
$Service = [Microsoft.Exchange.WebServices.Data.ExchangeService]::new()
$Service.Credentials = [Microsoft.Exchange.WebServices.Data.OAuthCredentials]$AccessToken
$Service.Url = “https://outlook.office365.com/EWS/Exchange.asmx”
$Service.AutodiscoverUrl($MailboxName,{$true})

It is throwing the following error:

Exception calling "AutodiscoverUrl" with arguments "2": "The Autodiscover service couldn't be located."
Online: 29 Character: 1
+ $Service.AutodiscoverUrl($MailboxName,{$true})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
     + FullyQualifiedErrorId : AutodiscoverLocalException

I hope you can help me

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,381 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
528 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,321 questions
{count} votes

Accepted answer
  1. Vasil Michev 100.2K Reputation points MVP
    2023-05-18T08:18:36.7166667+00:00

    You can just hardcode the EWS URL, it's the same for all Exchange Online mailboxes.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Amit Singh 4,956 Reputation points
    2023-05-18T10:35:28.85+00:00

    I will recommend to you enable Traces, to achieve this following:

    Service.TraceEnabled = true;

    I was facing the same issue then when I enabled traces. These traces will guide you on what exactly is happening. In my case, the SSL certificate issue is there to solve it. I followed the following post.

    There can be many issues, such as:

    Users can be blocked.

    The DNS can't find autodiscover.domain.com.


  2. Limitless Technology 44,116 Reputation points
    2023-05-18T13:41:10.3533333+00:00

    Hello there,

    For O365 mailboxes, you can safely hardcode the Autodiscover URL to "https://outlook.office365.com/EWS/Exchange.asmx"

    and avoid any lookup issues.

    AutoDiscoverv1 doesn't support the client credentials flow so you need to remove the line

    $ews.AutodiscoverUrl($MailboxName,{$true})

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--