首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EXO PowerShell V2模块远程会话Cryptography.SHA256Cng错误与.NET 5.0

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

Stack Overflow用户
提问于 2021-02-02 18:44:54
回答 1查看 1.3K关注 0票数 2

尝试在PowerShell 5.0项目中使用C#实现Exchange远程V2 (使用最新的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://learn.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell?view=exchange-ps

作为一个小测试,这些命令都可以正常工作(无论是使用拇指指纹还是直接使用.pfx ):

代码语言:javascript
复制
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -CertificateThumbprint "" -AppId "" -Organization ""
Get-EXOMailbox -Identity ""
Disconnect-ExchangeOnline

我使用这个远程运行空间示例作为基础,并试图通过C#:https://learn.microsoft.com/en-us/powershell/scripting/developer/hosting/creating-remote-runspaces?view=powershell-7.1修改它以执行相同的命令。

代码语言:javascript
复制
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.Automation 7.1.1

目标框架的.NET 5.0

此错误发生在使用命令的第二个powershell.Invoke()行上。

代码语言:javascript
复制
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之前使用该命令,因此模块似乎至少是正确导入的。

无法从微软找到更新的C#示例或指南。我是否应该使用不同的NuGet包,或者我缺少了其他什么东西?这样做更好?

会感谢您的任何见解,谢谢!

编辑:包括一些与此相关的链接。

https://learn.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核心支持的版本。更新到预览后没有密码错误,但仍在同一行上错误。

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-03 05:58:52

明白了,现在返回results对象中的数据。

以下是关于设置Azure应用程序并创建一个自签名证书。的一些信息

还有一些其他的事情需要做:

  1. 确保您使用的是支持PowerShell核心的模块版本。它目前处于预览阶段,您需要2.0.4-Preview2EXO PowerShell V2模块中的核心支持

最后,我执行了以下步骤以确保有正确的版本,关闭所有提示,然后打开管理员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确认。

  1. 不知道为什么会起作用,但随后出现了一个异常,并登陆了这个单独的问题

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

代码语言:javascript
复制
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
  </PropertyGroup>

就这样..。代码中没有什么不同,不需要添加UseWindowsPowerShell

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66015757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档