We have a code that successfully runs in a console app but fails in Azure Function. We're connecting to Exchange Online from Powershell in C# Azrue Function. It's using .NET 6.0 and Microsoft.PowerShell.SDK 7.1.10. Connection to Exchange Online is made with the latest module of ExchangeOnlineManagement 3.0 and a certificate. When we run "Connect-ExchangeOnline" from Azure Function, we get "System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'XXX'", where all missing parameters are in the following list: {AppId,Certificate,CertificateFilePath,CertificatePassword,CertificateThumbprint,Credential,EnableErrorReporting,LogDirectoryPath,LogLevel,ManagedIdentity,ManagedIdentityAccountId,Organization,PageSize,ShowProgress,TrackPerformance,UseMultithreading,UserPrincipalName}.
Inspecting ExchangeOnlineManagement.psm1 script, we can see that all these parameters are different from other parameters and added using the System.Management.Automation.RuntimeDefinedParameterDictionary. Is there something in Microsoft.NET.Sdk.Functions package that is preventing them from being loaded? Or is it by design?
How can we connect to Exchange Online from Azure Function using a certificate if we can't specify any of needed parameters, such as Organization, CertificateFilePath and AppID?
Sample of the code:
var defaultSessionState = InitialSessionState.CreateDefault();
defaultSessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
Runspace runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
runspace.Open();
_ps = PowerShell.Create();
_ps.AddScript("Import-Module -Name ExchangeOnlineManagement").Invoke();
var results = _ps.AddScript("(Get-Command Connect-ExchangeOnline).Parameters").Invoke();
The last line executed in Azure Function does not return any of the parameters mentioned above.
Thanks!