Forum Discussion
The term 'Get-MessageTraceV2' is not recognized as a name of a cmdlet
I am writing a function app to get message delivery status.
I have the package defined in requirements.psd1 as such:
# 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.*' 'ExchangeOnlineManagement' = '3.*' }
This is the error we get:
The term Get-MessageTraceV2 is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct, and try again.
The bizarre thing is that it sometimes works and sometimes doesn't without any changes to the code.
Even when several of my colleagues and I try the command locally, we have the same error.
3 Replies
- Aqeel-KhadimCopper Contributor
I’ve recently encountered the same issue while using Get-MessageTraceV2 within an Azure PowerShell Function App with Managed Identity.
The strange part is, it’s totally intermittent. Sometimes the cmdlet works perfectly, and then on the next run, PowerShell suddenly says it’s not recognized, even though the module is installed and imported correctly.
What I’ve learned so far:
The issue seems to come from the ExchangeOnlineManagement module not consistently loading all cmdlets when the session is initialized via Connect-ExchangeOnline -ManagedIdentity.
I’m also using 'ExchangeOnlineManagement' = '3.*' in requirements.psd1.
The permissions and roles don’t seem to make a difference (Exchange.ManageAsApp, Exchange Admin, Tracking-View-Only, etc.).What helped a bit on my end:
Connect-ExchangeOnline -ManagedIdentity -ShowBanner:$false
Import-Module ExchangeOnlineManagement -Force
Get-Command Get-MessageTraceV2 | Out-Null # Sanity check before runningIf the cmdlet still isn’t found, restarting the Function App usually reloads it, but that’s obviously not ideal. I suspect there’s a module load timing or session context issue in how the Exchange module is initialized in Azure Functions; it's not tied to permissions.
You’re definitely not alone in this one; several of us have been seeing the same sporadic behavior lately.
The way Exchange PowerShell works is by downloading the cmdlet definitions, as per the roles assigned to the current user. Thus, you will not find the Get-MessageTraceV2 cmdlet within the ExchangeOnlineManagement, but within the temporary module created upon connecting, i.e. tmpEXO_y4vgef2c.4jr.
Of course, you also need to check the permissions, for any given cmdlet you can find out which roles are appropriate via:
Get-ManagementRole -Cmdlet Get-MessageTraceV2
In the case of managed identities/service principals, you must that both the API permissions and the Exchange role are assigned. Lastly, Get-MessageTraceV2 is currently not available in GOV or any other instance, apart from the "standard" one.
- froggothegoodCopper Contributor
Extra Information:
I am using managed Identity to connect to exchange.
I have added Exchange.ManageAsApp permission.
I have tried: exchange admin, global reader, Tracking-View-Only (exchange role).
All the same.