Issues using managed dependencies in Azure Functions with PowerShell - ExchangeOnlineManagement

Michał Pawlicki 0 Reputation points
2025-01-22T14:52:47.9933333+00:00

Hi everyone, I am trying to deploy a simple Azure Function using PowerShell. However, I cannot load any modules. I have a module for ExchangeOnlineManagement I declared it in the requirements file. I also added managed depedencies to the host.json file and in the profile.PS1 I specified to import the module and to connect using managed identity, however when I tried to use get – mailbox it returns the information that the module was not found / couldn't be loaded.

I was trying to do the same with multiple different modules just to see if it's gonna work and it seems that I'm always failing with the error that the module was not found. I am completely lost. I thought that managed depends said to true would be enough and lastly, I'm on the dynamic pricing so to manage dependency should not be the problem by itself please help.

2025-01-22T14:45:26Z   [Verbose]   AuthenticationScheme: WebJobsAuthLevel was successfully authenticated.
2025-01-22T14:45:26Z   [Verbose]   AuthenticationScheme: Bearer was not authenticated.
2025-01-22T14:45:26Z   [Verbose]   Authorization was successful.
2025-01-22T14:45:26Z   [Information]   Executing 'Functions.HttpTriggerTest' (Reason='This function was programmatically called via the host APIs.', Id=0c36614c-f090-4a92-b957-2f34945ed26a)
2025-01-22T14:45:26Z   [Verbose]   Sending invocation id: '0c36614c-f090-4a92-b957-2f34945ed26a
2025-01-22T14:45:26Z   [Verbose]   Posting invocation id:0c36614c-f090-4a92-b957-2f34945ed26a on workerId:a6ec9bff-8b1f-4124-a2fc-88613c2c44f3
2025-01-22T14:45:26Z   [Warning]   The first managed dependency download is in progress, function execution will continue when it's done. Depending on the content of requirements.psd1, this can take a few minutes. Subsequent function executions will not block and updates will be performed in the background.
2025-01-22T14:46:01Z   [Error]   Executed 'Functions.HttpTriggerTest' (Failed, Id=0c36614c-f090-4a92-b957-2f34945ed26a, Duration=34982ms)
2025-01-22T14:46:01Z   [Verbose]   AuthenticationScheme: WebJobsAuthLevel was successfully authenticated.
2025-01-22T14:46:01Z   [Verbose]   AuthenticationScheme: Bearer was not authenticated.
2025-01-22T14:46:01Z   [Verbose]   Authorization was successful.
2025-01-22T14:46:01Z   [Information]   Executing 'Functions.HttpTriggerTest' (Reason='This function was programmatically called via the host APIs.', Id=a0e27ffd-2382-4719-bc61-3f367709f8e9)
2025-01-22T14:46:01Z   [Verbose]   Sending invocation id: 'a0e27ffd-2382-4719-bc61-3f367709f8e9
2025-01-22T14:46:01Z   [Verbose]   Posting invocation id:a0e27ffd-2382-4719-bc61-3f367709f8e9 on workerId:a6ec9bff-8b1f-4124-a2fc-88613c2c44f3
2025-01-22T14:46:01Z   [Warning]   The Function app may be missing the 'ExchangeOnlineManagement' module. If 'ExchangeOnlineManagement' is available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see https://aka.ms/functions-powershell-managed-dependency.
2025-01-22T14:46:02Z   [Error]   ERROR: The specified module 'ExchangeOnlineManagement' was not loaded because no valid module file was found in any module directory.



And as for what's happening in my profile.ps1:


# The tenant name (orgname.onmicrosoft.com) set in the Function App configuration
$tenant = $env:Tenant 

Import-Module ExchangeOnlineManagement 
Import-Module Az.Accounts
Import-Module Az.ManagedServiceIdentity

if ($env:MSI_SECRET -and (Get-Module -ListAvailable Az.Accounts)) {
    Connect-AzAccount -Identity
    Connect-ExchangeOnline -ManagedIdentity -Organization $tenant
}

$tokenAuthURI = $env:MSI_ENDPOINT + "?resource=https://management.azure.com&api-version=2017-09-01"
$env:tokenResponse = Invoke-RestMethod -Method Get -Headers @{"Secret"="$env:MSI_SECRET"} -Uri $tokenAuthURI

And run.ps1


# ok some magic here for exchange online
$user = Get-Mailbox -Identity dummy@dummy.com
$body = $user.ToString()

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

And requirements.psd1

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. Uncomment the next line and replace the MAJOR_VERSION, e.g., 'Az' = '5.*'
    # 'Az' = 'MAJOR_VERSION.*'
    'Az.Accounts' = '2.*'
    'Az.ManagedServiceIdentity' = '1.*'
    'ExchangeOnlineManagement' = '3.*'
    #'ExchangePowerShell' = '0.*'
    'PowerShellGet' = '2.*'
    'PackageManagement' = '1.*'   
}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,356 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,762 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 5,575 Reputation points Microsoft Employee
    2025-01-27T10:06:26.64+00:00

    Hello Michał Pawlicki

    If you have already added the module to the requirements.psd1 file, then it is possible that the module is not compatible with PowerShell 7. Please check if the module is compatible with PowerShell 7.

    Lastly, please check if the module is available on the PowerShell Gallery. If it is available, please add a reference to this module to requirements.psd1. You maybe using modules from older powershell versions.

    I hope this helps


Your answer

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