I tried automating exchange functions using runbook, using powershell 5.1 runtime but ended up getting unauthorized error

Sakthivel Ganesan 0 Reputation points
2025-12-12T02:56:14.9333333+00:00

Hi,

I wanted to automate shared mail box creation, so i used azure automation runbook with exchangeonline management module, earlier i was facing issue with 7.2 runtime so i downgraded my powershell to 5.1, but still iam getting unauthorized error, can anyone help me with the issue

Service: Azure Automation
Type: Runbook
Runtime: Powershell 5.1
Module: EchangeOnline Management(3.9.0)

Exchange Online
Exchange Online
A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jade-T 8,680 Reputation points Microsoft External Staff Moderator
    2025-12-12T04:55:35.51+00:00

    Hi @Sakthivel Ganesan

    Thank you for posting your question in the Q&A Forum. 

    Receiving an “Unauthorized” error while using the Exchange Online Management module even after switching to PowerShell 5.1 is a common scenario in Azure Automation. In many cases, this points to an authentication or permissions issue rather than a problem with the runtime itself. 

    Here are some areas you might consider reviewing: 

    1. Check Permissions of the Runbook Identity 

    The identity used by your Runbook (whether a Managed Identity or a Service Principal) must have the appropriate administrative rights in Exchange Online. 

    It may be helpful to verify that the Runbook identity is assigned to an Exchange Online role group that allows shared mailbox creation. Commonly required roles include: 

    • Recipient Management 
    • Exchange Administrator 
    • Organization Management 

    Without these permissions, Exchange Online may return an Unauthorized error even if the connection appears to succeed. 

    2. Non-Interactive Authentication 

    Standard username/password login is generally not supported in Azure Automation, particularly if Multi-Factor Authentication (MFA) is enabled, as this can prevent the connection from succeeding. 

    For automated scripts, it is usually recommended to use one of the following secure, non-interactive methods: 

    • Managed Identity: You can connect using a System‑Assigned Managed Identity with the following command: 
    Connect-ExchangeOnline –ManagedIdentity 
    

    For detailed guidance on using Managed Identity with Exchange Online PowerShell, refer to the official Microsoft documentation: Use Azure managed identities to connect to Exchange Online PowerShell | Microsoft Learn 

    App Access Policies 
    Even when the Runbook identity has the correct permissions and uses a supported authentication method, App Access Policies may still restrict the connection. 

    If your organization has App Access Policies configured, ensure that the identity used by the Runbook is explicitly allowed. Otherwise, the connection may be blocked without a clear error message. 

    Focusing on these checks may help the error: 

    • Verify the Runbook identity has the required Exchange Online permissions
    • Confirm the Runbook is using a supported non-interactive authentication method
    • Check if App Access Policies are restricting the identity. 

    These steps typically resolve Unauthorized errors in automation scenarios. Updating permissions or switching to a supported authentication method often resolves the issue. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.