I used to use Basic authentication when connecting to Exchange online, but after Microsoft disabled the basic authentication I have not had luck connecting to it. Having it use authentication popup would be ideal.
Code looked something like this:
PSCredential credential = new PSCredential(username, new NetworkCredential("", password).SecurePassword);
WSManConnectionInfo wsEOConnInfo = new WSManConnectionInfo((new Uri("https://outlook.office365.com/powershell-liveid/")),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
wsEOConnInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
try
{
using (Runspace runspace = RunspaceFactory.CreateRunspace(wsEOConnInfo))
{
runspace.Open();
try
{
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
PowerShell ps = PowerShell.Create();
var getLists = new PSCommand();
getLists.AddCommand("Get-DistributionGroup");
ps.Commands = getLists;
ps.Runspace = runspace;
var response = ps.Invoke();
}
}
}
}
I tried replacing the Basic authentication part with Powershell calls, that should bring up external authentication, but that doesn't seem to work with the C# sdk.
Connect-ExchangeOnline -UserPrincipalName "[email protected]"
this just creates error, and even tried to run Install-Module and Import-Module commands, but in the SDK Install-Module command is unknown and Import doesn't help
The error is:
System.Management.Automation.CommandNotFoundException: 'The term 'Get-DistributionGroup' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'
Is there any way to get access to Exchange Online commands inside C# or do I need to rewrite it all in PowerShell for it to work.
To summarize the problem and provide a sample solution:
You were looking for an alternative to using a remote PowerShell runspace, because your authentication method (
Basic) is no longer supported.Trying to use a local runspace with a call to
Connect-ExchangeOnlineto establish the remote connection encountered two problems:Your PowerShell SDK project didn't see the required
ExchangeOnlineManagementmodule, and such projects do not come bundled withInstall-Modulefor on-demand installation.Additionally, PowerShell SDK projects do not support PowerShell's interactive features, such as
Get-CredentialandRead-Host, thereby preventing prompting for credentials.Workarounds can take advantage of the fact that using native .NET features for user interaction is not subject to this limitation, so you can create your own credentials prompt and pass the result to PowerShell later.
For instance, this answer - whose code is integrated below - shows how to present a console-based, masked password prompt that returns a
SecureStringinstance; alternatively, you can create a GUI dialog.Simple sample code that demonstrates console-based up-front prompting for a password, constructing a
PSCredentialinstance, and passing it to PowerShell, written for the PowerShell (Core) SDK (Microsoft.PowerShell.SDKand .NET 7+: