[英]Azure Function with authentication to Dynamic 365 - Could not load type 'System.Security.Cryptography.SHA256Cng'
[英]EXO PowerShell V2 Module remote session Cryptography.SHA256Cng error with .NET 5.0
尝试使用 C# 在 Z303CB0EF9EDB90872AZBBBBBB 项目中实施 Exchange Online Remote PowerShell(使用最新的 EXO PowerShell V2 模块)。 我过去使用基本身份验证,并选择为非交互式脚本切换到基于证书的身份验证。 背景明智,我有 Azure 应用程序和证书都设置和工作。
按照下面的指导,我可以使用 PowerShell 提示符手动连接。
作为一个小测试,这些命令都可以正常工作(直接使用指纹或.pfx):
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -CertificateThumbprint "" -AppId "" -Organization ""
Get-EXOMailbox -Identity ""
Disconnect-ExchangeOnline
我使用这个远程运行空间示例作为基础并尝试修改它以通过 C# 执行相同的命令: https://docs.microsoft.com/en-us/powershell/scripting/developer/hosting/creating-remote-runspaces?查看=powershell-7.1
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
{
remoteRunspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = remoteRunspace;
powershell.AddCommand("Import-Module");
powershell.AddParameter("Name", "ExchangeOnlineManagement");
powershell.Invoke();
powershell.Commands.Clear();
powershell.AddCommand("Connect-ExchangeOnline");
powershell.AddParameter("AppId", "");
powershell.AddParameter("CertificateThumbprint", "");
powershell.AddParameter("Organization", "");
powershell.Invoke();
powershell.Commands.Clear();
powershell.AddCommand("Get-EXOMailbox");
powershell.AddParameter("Identity", "");
powershell.Invoke();
Collection<PSObject> results = powershell.Invoke();
powershell.Commands.Clear();
powershell.AddCommand("Disconnect-ExchangeOnline");
powershell.Invoke();
}
remoteRunspace.Close();
}
在项目中,据我所知,我使用的是所有最新的 NuGet 包。
Microsoft.PowerShell.SDK
.PowerShell.SDK 7.1.1
System.Management.Automation
7.1.1
.NET 5.0
用于目标框架
此错误发生在使用 Connect-ExchangeOnline 命令的第二个powershell.Invoke()
行上。
System.Management.Automation.RuntimeException: 'Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=....'.. '
包括 Import-Module 的UseWindowsPowerShell
参数没有帮助。 如果我删除与Connect-ExchangeOnline
关联的行,则会在Get-EXOMailbox
之前提到使用该命令的错误,因此该模块似乎至少可以正确导入。
无法找到更新的 C# 示例或来自 Microsoft 的指导。 我应该使用不同的 NuGet 包,还是我还缺少其他东西? 更好的方法来做到这一点?
将不胜感激任何见解,谢谢!
编辑:包括一些有些相关的链接。
https://github.com/Azure/azure-functions-powershell-worker/issues/503#issuecomment-670676511
编辑 2 :返回并确保我安装了模块的2.0.4-Preview2
版本,因为这是唯一支持 PowerShell 核心的模块。 更新到预览版后没有加密错误,但仍然在同一行出错。
Inner Exception 1:
PSRemotingDataStructureException: An error has occurred which PowerShell cannot handle. A remote session might have ended.
Inner Exception 2:
NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.
明白了,现在在results
object 中取回数据。
以下是有关设置 Azure 应用程序和创建自签名证书的一些信息。
还有一些其他的事情需要做:
确保您使用的模块版本支持 PowerShell 内核。 它目前处于预览状态,您需要2.0.4-Preview2
。 EXO V2 模块中的 PowerShell 核心支持。
我最终执行了以下步骤以确保我拥有正确的版本,关闭所有提示,然后打开管理员 PowerShell 提示:
Uninstall-Module -Name ExchangeOnlineManagement
并关闭提示。
Install-Module PowerShellGet –Repository PSGallery –Force
in a new prompt,然后关闭。
Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.4-Preview2 -AllowPrerelease
Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement
进行确认。
不知道为什么这会奏效,但遵循一个例外并落在这个单独的问题上。
在项目文件中,我做了与接受的答案相同的操作。
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
就是这样..代码没有什么不同,不必添加UseWindowsPowerShell
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.