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.
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://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();
}
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://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.