Manage Office 365 Using PowerShell From A Docker Container

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.

Deployment Steps
  • 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
Files Used

My DockerFile will use the files below to create the Image.

DockerFile

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

Build Image

From my DockerFile folder, I’ll run the building line below to create my Docker Image

docker build -t office365 .

Run Container

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

Cd install

To connect to Office 365, I’ll call the ConnectOffice365.ps1 script and provide a username and password

ConnectOffice365.ps1

Once authenticated I can use any cmdlet In the Office 365 MSonline PowerShell module and seen below

Get-MsolAccountSku

Conclusion

Connecting to Office 365 from a Docker Container Is much easier, portable and secure solution than Installing it on a management machine.

What’s Next

In the next article, I’ll add the option to Connect to Office 365 using the new AzureAD PowerShell which will replace the MSOnline module.

2 Replies to “Manage Office 365 Using PowerShell From A Docker Container”

  1. Hi

    Nice blog post, though i got stuck a bit ,couldnt get mkdir to work with subdirectory so basicly changed the code for that to my ex.

    RUN powershell.exe New-Item -type Directory -path c:\install\,c:\install\365

    and for copy file and execute i had to include “” in the path

    You maybe have any smother way to do it?

Leave a Reply

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