At the time this article was written, this documentation had not been updated since September 2015. Although it is useful as a reference, it describes a connection to a remote runspace. In simple terms, a remote runspace runs commands on an Exchange server or an Exchange Online server. It is therefore subject to various security restrictions. For example, common scripting syntax such as variables and assignment expressions is not available.
A local runspace, which does not have those restrictions, is needed instead, but no sample code for this approach has been published. The steps are nevertheless the same as when connecting to Exchange Online from Windows PowerShell. With the traditional Basic authentication connection, New-PSSession and Import-PSSession import the Exchange Online commands into the local runspace. When using the Exchange Online PowerShell V2 module, Connect-ExchangeOnline runs New-PSSession and Import-PSSession internally.
This article explains how to connect to Exchange Online from C# using the Exchange Online Remote PowerShell V2 module. It assumes familiarity with Visual Studio and C# application development, so it does not provide detailed step-by-step instructions.
Environment
The sample was tested in the following environment:
Visual Studio 2019
.NET 5.0
PowerShell 7.1.3
ExchangeOnlineManagement PowerShell module 2.0.5
Microsoft.PowerShell.SDK 7.1.3
System.Management.Automation 7.1.3
The runtime environment is .NET 5.0, so the sample uses PowerShell 7, which runs on .NET. Windows PowerShell, which runs on .NET Framework, cannot be used in this environment.
This sample is not intended to run in Azure Functions. Connecting to Exchange Online PowerShell from Azure Functions might work, but it is not strictly supported. The ExchangeOnlineManagement PowerShell module is intended to run on client computers and might not work correctly in Azure Functions.
Prerequisites
Both the development and production environments must already be configured to connect using the ExchangeOnlineManagement PowerShell module. Refer to the documentation to prepare the environment. If the account running the program differs from the user installing the module, install it with the Install-Module command’s Scope parameter set to AllUsers.
using Microsoft.PowerShell; using System; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Remoting; using System.Management.Automation.Runspaces; using System.Security; using System.Security.Cryptography.X509Certificates; using System.Text;
namespaceExchangeOnlinePowerShellNet { classProgram { staticvoidMain(string[] args) { // Connect Exchange Online PowerShell using V2 module. // ExchangeOnlineManagement PowerShell module need to be installed.
// If you don't know which PowerShell Nuget Packages you should use, read this blog. // https://devblogs.microsoft.com/powershell/depending-on-the-right-powershell-nuget-package-in-your-net-project/ // You need to install Microsoft.PowerShell.SDK 7.1.x and System.Management.Automation 7.1.x when you use PowerShell 7 and .NET 5.0.
V2ModuleExchangeOnlineRemotePowerShell();
Console.ReadLine(); }
publicstaticvoidWriteStreams(PSDataStreams Streams) { // Write Error, Warning and Information stream
results = shell.Invoke(); foreach (PSObject result in results) { Console.WriteLine(result.Properties["UserPrincipalName"].Value.ToString()); }
WriteStreams(shell.Streams); } } } } }
The code implements the following four methods provided by Connect-ExchangeOnline. In V2ModuleExchangeOnlineRemotePowerShell, comment out all but the connection method you want to try:
UPN and password (ConnectExchangeOnlineUsingUserPrinsipalNameAndPassword)
Start debugging. If it runs successfully, the results of Get-Mailbox and Get-EXOMailbox are displayed.
Additional explanation
The WriteStreams method writes the executed command’s error, warning, and information streams to the console. Standard output is written separately for each command where needed.
Each of the four connection methods requires environment-specific information. Change the variables, such as the tenant name, to match your environment.
The sample specifies PSSessionOption so that Connect-ExchangeOnline uses the IE proxy settings. This setting is not necessarily required in an environment without a proxy server. When collecting a Fiddler trace for troubleshooting, it allows Exchange Online traffic to go through Fiddler as well.