本地运行Azure函数应用导入ExchangeOnline模块时Microsoft.PowerShell.Utility加载失败求助
我有一个本地可正常运行的控制台应用,通过CertificateThumbprint、AppID、Organization凭证连接ExchangeOnline。但将其转为Azure函数应用并本地运行时,导入ExchangeOnlineManagement模块时出现以下错误:
[2024-10-07T03:46:44.417Z] Error during Import-Module: The module 'Microsoft.PowerShell.Utility' could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Utility'
尝试添加导入该模块的操作后,又出现新错误:
Exception: Cannot find the built-in module 'Microsoft.PowerShell.Utility' that is compatible with the 'Core' edition. Please make sure the PowerShell built-in modules are available. They usually come with the PowerShell package under the $PSHOME module path, and are required for PowerShell to function properly
使用的SDK版本为7.4.5,以下是连接ExchangeOnline的C#函数代码:
public static Task GetUserMailbox(string appId, string certificatePath, string certificatePassword, string organization, string userPrincipalName, ILogger log) { try { // Set TLS protocol to 1.2 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; // Optionally bypass SSL certificate validation (not recommended for production) System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; // Load the certificate from the file using the password var certificate = new X509Certificate2(certificatePath, certificatePassword); var iss = InitialSessionState.CreateDefault2(); iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass; using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(iss)) { remoteRunspace.Open(); // Ensure the Runspace is opened using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = remoteRunspace; // Import the Exchange Online module powershell.AddCommand("Import-Module") .AddArgument("ExchangeOnlineManagement") .Invoke(); // Check for errors in Import-Module if (powershell.HadErrors) { foreach (var error in powershell.Streams.Error) { log.LogError($"Error during Import-Module: {error}"); } return Task.CompletedTask; // Stop further execution if the module import fails } log.LogInformation("Successfully imported ExchangeOnlineManagement module."); // Clear the previous command powershell.Commands.Clear(); // Connect to Exchange Online powershell.AddCommand("Connect-ExchangeOnline") .AddParameter("AppId", appId) .AddParameter("CertificateThumbprint", certificate.Thumbprint) .AddParameter("Organization", organization) .AddParameter("ShowBanner", false); powershell.Invoke(); // Check for errors in Connect-ExchangeOnline if (powershell.HadErrors) { foreach (var error in powershell.Streams.Error) { log.LogError($"Error during Connect-ExchangeOnline: {error}"); } return Task.CompletedTask; // Stop further execution if connection fails } log.LogInformation("Successfully connected to Exchange Online."); // Get user mailbox details using Get-CASMailbox and Select-Object powershell.Commands.Clear(); powershell.AddCommand("Get-CASMailbox") .AddParameter("Identity", userPrincipalName) .AddCommand("Select-Object") .AddParameter("Property", new string[] { "DisplayName", "UserPrincipalName", "ForwardingSmtpAddress", "OWAEnabled" }); var mailboxResults = powershell.Invoke(); // Check for errors in Get-CASMailbox if (powershell.HadErrors) { foreach (var error in powershell.Streams.Error) { log.LogError($"Error during Get-CASMailbox: {error}"); } return Task.CompletedTask; // Stop if mailbox retrieval fails } // Display results if (mailboxResults != null && mailboxResults.Count > 0) { foreach (var result in mailboxResults) { log.LogInformation(result.ToString()); // Safely convert PSObject to string } } else { log.LogInformation("No mailbox found or no results returned."); } // Disconnect from Exchange Online powershell.Commands.Clear(); powershell.AddCommand("Disconnect-ExchangeOnline") .AddParameter("Confirm", false); powershell.Invoke(); // Check for errors in Disconnect-ExchangeOnline if (powershell.HadErrors) { foreach (var error in powershell.Streams.Error) { log.LogError($"Error during Disconnect-ExchangeOnline: {error}"); } } else { log.LogInformation("Successfully disconnected from Exchange Online."); } } } } }
内容的提问来源于stack exchange,提问作者Earvin Nill Castillo

