Connect to Exchange Online From A Docker Container

In this article, I’ll show you how I build a Docker Container that will connect to Exchange Online.

Following the success of my Office 365 PowerShell container Image, I’ve decided to build another Docker Image that will connect to Exchange Online.

When managing multiple Exchange Online environments, It’s always a bit hard to manage connection sessions when using a management machine and sometimes you can make changes in the wrong environment.

This Image will remove this risk and make the connection process more secure and safe when managing multiple environments or a single environment.

About the build

The Image Is based on Windows Server Core 2016 operating system with the PowerShell connection script that asks for a username and password.

PS Code

Below, you can see the connection script to Exchange Online, The script Is located on the C:\ Drive.

$powerUser = read-host "Enter Username"
$password = read-host "Enter Password" -AsSecureString
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password
$O365Cred = $adminCredential
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $O365Cred -Authentication "Basic" -AllowRedirection
Import-PSSession $ExchangeSession
Docker File

And below, you can see the DockerFile that makes all the magic.

FROM microsoft/windowsservercore
RUN powershell.exe
COPY Connect.ps1 c:\

Run Container

Once the Image Is completed, I’ll spin a new Container.

docker run -it --rm ntweekly/exchangeonline powershell

To run the Connection script, I’ll start the connection script from the C: drive.

The connection script Is called Connect.ps1

Docker Hub

If you want to download the Image, please use command below to pull it from Docker Hub

Docker pull ntweekly/exchangeonline

Leave a Reply

Your email address will not be published. Required fields are marked *