How is it possible to add a user to Distribution List via Add-DistributionGroupMember from an Azure Automation Account?

Tyler Wakefield 30 Reputation points
2026-06-25T17:57:29.63+00:00

I have a PowerShell script that works both from PowerShell ISE (PS version 5.1.26100.8655) and VS Code (PS version 7.6.2) and successfully adds the designated user to a distribution list. I am now trying to get this script to run from an Automation Account so that I can invoke it from a Power Automate workflow later. However, I have been unable to get the script to run either in a PowerShell 5.1 or 7.2 runtime environment in my Azure subscription.

The Exchange Online Management module refuses to load in the 7.2 environment. I've tried manually packaging version 3.5.0 as suggested by many posts but every time I get an error that the specified module cannot be located even when the module list for the environment shows it is loaded and in an "Available" state. I have also tried downloading the file from the PS gallery and loading that but the same issue occurs.

I can get the module to load normally in the 5.1 runtime environment but every time I run the script it returns an error "Maximum retry count reached." I have confirmed that the application running the script has

Exchange.ManageAsApp permissions and is assigned as an Exchange Administrator. It seems like this should be a relatively straightforward task to automate but I cannot figure out how to get this script to execute properly.

Import-module MSAL.PS 
Import-module Microsoft.Graph.Authentication
Import-module Microsoft.Graph.Users
Import-module ExchangeOnlineManagement 

$applicationID = Get-AutomationVariable -Name "ApplicationID"
$applicationSecret = Get-AutomationVariable -Name "ApplicationSecret" | ConvertTo-SecureString -AsPlainText -Force
$tenantId = Get-AutomationVariable -Name "TenantID"

$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential `
    -ArgumentList $applicationID, $applicationSecret

Try {
    
    Connect-MgGraph -tenant $tenantId -ClientSecretCredential $ClientSecretCredential
    
    #Get the User ID (using User Principal Name)
    $User = Get-MgUser -UserId $username
    $UserId = $User.Id


    #Generate token to connect to Exchange
    $details = @{
    'TenantId'     = $tenantId # Directory (tenant) ID
    'ClientId'     = $applicationID # Application (client) ID
    'ClientSecret' = $applicationSecret 
    'Scope'        = "https://outlook.office365.com/.default"
    }

    $token = Get-MsalToken @details

    Connect-ExchangeOnline -AccessToken $token.AccessToken -Organization "pereviewsoftware.com"

    #Add the user to the group
    Add-DistributionGroupMember -Identity $groupName -Member $UserId -ErrorAction Stop

    $result = @{
        Status = "Success"
        Message = "$username has been added to $groupName"
    }
}
Catch {
        $result = @{
        Status = "Error"
        ErrorMessage = $_.Exception.Message
        StackTrace = $_.ScriptStackTrace
        }
} Finally {
    Write-output ($result | ConvertTo-Json -Depth 10)
}

Azure Automation
Azure Automation

An Azure service that is used to automate, configure, and install updates across hybrid environments.


Answer accepted by question author

Alex Burlachenko 25,030 Reputation points MVP Volunteer Moderator
2026-06-26T09:12:32.0033333+00:00

hi Tyler Wakefield & thx for sharing urs issue here at Q&A portal,

For this job, I’d avoid the ExchangeOnlineManagement module in Azure Automation if possible.

Add-DistributionGroupMember is an Exchange cmdlet, so it depends on Exchange Online PowerShell session behavior. Azure Automation + EXO module version/runtime combos can be painful, esp PS 7.2. The Maximum retry count reached error usually means the EXO REST/remote session connection is failing/retrying, not that the group add itself is wrong.

Since u already have app-only auth, the cleaner route is Microsoft Graph if the target is a Microsoft 365 group. But if it’s a classic Exchange distribution list, Graph won’t fully replace Add-DistributionGroupMember. That’s the annoying split.

For classic DLs, use Exchange Online PowerShell app-only auth with certificate auth instead of client secret/access token. That’s the more supported automation pattern

Connect-ExchangeOnline `

-AppId $applicationID `

-CertificateThumbprint $certThumbprint `

-Organization 'pereviewsoftware.com'

Add-DistributionGroupMember -Identity $groupName -Member $username

A couple things I’d change in ur script

Use $username or user UPN for -Member, not the Graph object id. Exchange cmdlets usually resolve mail-enabled identity better by SMTP/UPN.

Don’t mix MSAL token + Connect-ExchangeOnline -AccessToken unless u really have to. In Automation, cert-based Connect-ExchangeOnline is less cursed.

For PS 7.2 in Azure Automation, make sure the EXO module version is supported in that runtime and imported into the Runtime environment, not just the Automation Account legacy modules area. Module UI saying Available doesn’t always mean that runtime can load it. Cute, right.

If this must be triggered from Power Automate, Power Automate > webhook > Azure Automation runbook is fine. Just keep the runbook auth as EXO app-only certificate auth. That should be way more stable than trying to hand-roll tokens for Exchange.

rgds,

Alex

&

If my answer was helpful pls mark it and additional thx if u follow me at Q&A portal

and at my blog https://ctrlaltdel.blog/

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.