问题描述
尝试在 .NET 5.0 项目中使用 C# 实现 Exchange Online 远程 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?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.1System.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=....'.. '
为导入模块包含 UseWindowsPowerShell
参数没有帮助。如果我删除与 Connect-ExchangeOnline
关联的行,则会出现错误提示在 Get-EXOMailBox
之前使用该命令,因此该模块似乎至少可以正确导入。
无法从 Microsoft 找到更新的 C# 示例或指南。我应该使用不同的 NuGet 包,还是我缺少其他东西?更好的方法来做到这一点?
希望得到任何见解,谢谢!
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.
的一些信息还有一些其他的事情需要做:
-
确保您使用的模块版本支持 PowerShell Core。它目前处于预览状态,您需要
2.0.4-Preview2
。 PowerShell 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
确认。 -
不知道为什么会这样,但出现异常并登陆 this separate issue。
在项目文件中,我做了与接受的答案相同的操作。
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
就是这样..代码没有什么不同,不必添加UseWindowsPowerShell
。