.NET 5.0 的 EXO PowerShell V2 模块远程会话 Cryptography.SHA256Cng 错误

EXO PowerShell V2 Module remote session Cryptography.SHA256Cng error with .NET 5.0

尝试在 .NET 5.0 项目中使用 C# 实施 Exchange Online Remote PowerShell(使用最新的 EXO PowerShell V2 模块)。我过去使用基本身份验证,并选择切换到非交互式脚本的基于证书的身份验证。背景方面,我已设置好 Azure 应用程序和证书并正常运行。

按照下面的指导,我可以使用 PowerShell 提示手动连接。

https://techcommunity.microsoft.com/t5/exchange-team-blog/modern-auth-and-unattended-scripts-in-exchange-online-powershell/ba-p/1497387

https://docs.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell?view=exchange-ps

作为小测试,这些命令都可以正常工作(直接使用指纹或 .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?view=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 7.1.1
System.Management.Automation7.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=....'.. '

包括导入模块的 UseWindowsPowerShell 参数没有帮助。如果我删除与 Connect-ExchangeOnline 关联的行,则会出现在 Get-EXOMailbox 之前使用该命令的错误提示,因此该模块似乎至少可以正确导入。

无法从 Microsoft 找到更新的 C# 示例或指南。我应该使用不同的 NuGet 包,还是我还缺少其他东西?更好的方法吗?

希望有任何见解,谢谢!

编辑:包括一些有点相关的链接。

https://docs.microsoft.com/en-us/answers/questions/236631/could-not-load-type-39systemsecuritycryptographysh.html

https://github.com/Azure/azure-functions-powershell-worker/issues/503#issuecomment-670676511

编辑 2:返回并确保我安装了模块的 2.0.4-Preview2 版本,因为这是唯一支持 PowerShell Core 的模块。更新到预览后没有密码错误,但仍然在同一行出错。

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 对象中取回数据。

这是关于 setting up the Azure app and creating a self-signed certificate.

的一些信息

还有一些其他事情需要完成:

  1. 确保您使用的模块版本支持 PowerShell Core。它目前处于预览阶段,您需要 2.0.4-Preview2PowerShell Core support in the EXO V2 module

    我最终执行了以下步骤以确保我拥有正确的版本,关闭所有提示,然后打开管理员 PowerShell 提示:

    Uninstall-Module -Name ExchangeOnlineManagement 并关闭提示。
    Install-Module PowerShellGet –Repository PSGallery –Force 在新提示中,然后关闭。
    Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.4-Preview2 -AllowPrerelease
    Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement确认。

  2. 不确定为什么会这样,但遇到异常并登陆

在项目文件中,我做了与接受的答案相同的操作。

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
  </PropertyGroup>

就是这样。代码中没有什么不同,不必添加 UseWindowsPowerShell