尝试在PowerShell 5.0项目中使用C#实现Exchange远程V2 (使用最新的EXO PowerShell V2模块)。我在过去使用过基本身份验证,并选择切换到基于证书的身份验证,用于非交互式脚本。背景方面,我有Azure申请和证书全部设置和工作。
按照下面的指导,我可以使用PowerShell提示符手动连接。
作为一个小测试,这些命令都可以正常工作(无论是使用拇指指纹还是直接使用.pfx ):
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修改它以执行相同的命令。
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()行上。
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://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.发布于 2021-02-03 05:58:52
明白了,现在返回results对象中的数据。
以下是关于设置Azure应用程序并创建一个自签名证书。的一些信息
还有一些其他的事情需要做:
2.0.4-Preview2。EXO 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确认。
在项目文件中,我做了与接受的答案相同的操作。
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>就这样..。代码中没有什么不同,不需要添加UseWindowsPowerShell。
https://stackoverflow.com/questions/66015757
复制相似问题