In this blog post, I’ll show you how I build a Windows Container Image with all the needed Office 365 PowerShell components and connect to Office 365 using PowerShell.
The Image will use the latest Windows Server Core 2016 Container Image running on Windows Server 2016 Container host, however, you can also run this on a windows 10 with Docker for Windows Installed.
To connect to Office 365 using PowerShell I’ll Install the latest Microsoft Online Services Sign-In Assistant and Azure Active Directory Module.
Create a Container Image using DockerFile with all the components Installed (Microsoft Online Services Sign-In Assistant and Azure Active Directory Module)
Use a PowerShell Script to Connect to Office 365 (ConnectOffice365.Ps1)
Run a Windows Container from the Image
Connect To Office 365 Script
Because a Windows Container cannot prompt and start a Login Screen, I’ll use a PowerShell script that will be copied to the Container Image from the DockerFile code and handle the connection.
Note: In this post, I’m using the MSonline PowerShell module (version 1) and not the AzureAD (version 2) because It has all the cmdlets for Office 365 management.
Below, you can see the script which will ask for a Username and Password and will use the Connect-MsolService to connect to Office 365
$powerUser = read-host "Enter Username" $password = read-host "Enter Password" -AsSecureString $adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password $O365Cred = $adminCredential Connect-MsolService –Credential $O365Cred
My DockerFile will use the files below to create the Image.
Below, you can see my DockerFile and how I Install the Microsoft Online Services Sign-In Assistant and the Azure Active Directory Module.
I’m also copying the ConnectOffice365.Ps1 script to C:\Install
FROM microsoft/windowsservercore RUN powershell.exe RUN powershell.exe mkdir c:\install COPY ConnectOffice365.ps1 c:\install COPY AdministrationConfig_3.msi c:\install COPY msoidcli_64.msi c:\install RUN msiexec /i c:\install\AdministrationConfig_3.msi RUN msiexec /i c:\install\msoidcli_64.msi
From my DockerFile folder, I’ll run the building line below to create my Docker Image
docker build -t office365 .
To run my Container, I’ll run the code below, Also note that I’m using –rm to delete the Container Image on exit (you can remove it If you don’t want to delete it on exit)
docker run --rm -it office365 powershell.exe
Connect to Office 365
To connect to Office 365, I’ll open the Install folder on the C drive on the Container
To connect to Office 365, I’ll call the ConnectOffice365.ps1 script and provide a username and password
Once authenticated I can use any cmdlet In the Office 365 Msonline PowerShell module and seen below
Connecting to Office 365 from a Docker Container Is much easier, portable and secure solution than Installing it on a management machine.