簡體   English   中英

EXO PowerShell V2 模塊遠程 session Cryptography.SHA256Cng 錯誤與 .NET 5.0

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

嘗試使用 C# 在 Z303CB0EF9EDB90872AZBBBBBB 項目中實施 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?查看=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://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 核心的模塊。 更新到預覽版后沒有加密錯誤,但仍然在同一行出錯。

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 應用程序和創建自簽名證書的一些信息。

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

  1. 確保您使用的模塊版本支持 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進行確認。

  2. 不知道為什么這會奏效,但遵循一個例外並落在這個單獨的問題上

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

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

就是這樣..代碼沒有什么不同,不必添加UseWindowsPowerShell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM