简体   繁体   中英

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

Trying to implement Exchange Online Remote PowerShell (using the most recent EXO PowerShell V2 module) with C# in a .NET 5.0 project. I used Basic Authentication in the past and have opted to switch to Cert Based Authentication for the non-interactive scripts. Background wise, I have the Azure application and certificate all set up and working.

Following the guidance below, I am able to manually connect using a PowerShell prompt.

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

As a small test, these commands all work fine (either using the thumbprint or the.pfx directly):

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -CertificateThumbprint "" -AppId "" -Organization ""
Get-EXOMailbox -Identity ""
Disconnect-ExchangeOnline

I used this remote runspace example as a base and tried to modify it to perform the same commands via 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();
}

In the project, I am using all of the latest NuGet packages as far as I know.

Microsoft.PowerShell.SDK 7.1.1
System.Management.Automation 7.1.1
.NET 5.0 for target framework

This error occurs on the second powershell.Invoke() line with the Connect-ExchangeOnline command.

System.Management.Automation.RuntimeException: 'Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=....'.. '

Including the UseWindowsPowerShell parameter for the Import-Module did not help. If I remove the lines associated with Connect-ExchangeOnline , an error mentions to use that command before Get-EXOMailbox , so the module seems to be importing correctly at least.

Have been unable to find an updated C# example or guidance from Microsoft. Are there different NuGet packages I should be using, or is there something else I am missing? Better way to do this?

Would appreciate any insight, thanks!

Edit : Including a few somewhat related links.

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

Edit 2 : Went back and made sure I had version 2.0.4-Preview2 of the module installed since that is the only one with PowerShell Core support. No cryptography error after updating to the preview, but still erroring on the same line.

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.

Got it, getting data back in the results object now.

Here is some info on setting up the Azure app and creating a self-signed certificate.

There were a few other things that needed to be done:

  1. Make sure you are using a version of the module that supports PowerShell Core. It is currently in preview and you need 2.0.4-Preview2 . PowerShell Core support in the EXO V2 module .

    I ended up doing the following steps to make sure I had the correct version, close all prompts and then open an administrator PowerShell prompt:

    Uninstall-Module -Name ExchangeOnlineManagement and close the prompt.
    Install-Module PowerShellGet –Repository PSGallery –Force in a new prompt, then close.
    Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.4-Preview2 -AllowPrerelease
    Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement Import-Module ExchangeOnlineManagement; Get-Module ExchangeOnlineManagement to confirm.

  2. Not sure why this worked, but followed an exception and landed on this separate issue .

In the project file, I did the same as that accepted answer.

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

And that was it.. nothing different in the code, did not have to add UseWindowsPowerShell .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM