Powershell connect to O365/M365 on Fedora Linux (Update)

(Update) Small update to this post. I changed the recommendation to the “alternative way”. I think it works much better, since the release of V3.

The first real post will be a short one.

Last week there was an issue on a customers M365 tenant, which required a powershell connection to fix it. So fired up powershell on linux and tried to connect. which threw out an error that “WSMan” was missing. I had this issue once before and forgot how to fix it.

So after asking google again, I will put a guide together on how to install powershell and the WSMan module on Fedora. This should work on most linux distributions, except for the first and second parts. Those are distro specific.

I am using Fedora 34 KDE Plasma for this. Let’s start.

Installation and connection

  1. First, we need to register the Microsoft signature key.
fedora-kde :: ~ » sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
fedora-kde :: ~ » curl https://packages.micro10soft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

2. Install powershell

fedora-kde :: ~ » sudo dnf install powershell

Now at this point, you can start powershell with the “pwsh” command and try to connect to O365, but you will probably get an error message that looks like this.

This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system.

Let’s solve this.

3. Install WSMan Modules in powershell

fedora-kde :: ~ » sudo pwsh -Command "Install-Module -Name PSWSMan"
fedora-kde :: ~ » sudo pwsh -Command "Install-WSMan"

Now the connection to O365/M365 should work. Let’s try it.

4. Start Powershell

fedora-kde :: ~ » pwsh

5. Write your credentials into a variable.

PS /home/user> $UserCredential = Get-Credential

6. Start the Session to O365/M365

PS /home/user> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

7. Import the modules for Exchange Online. If you get an error the first time, just retry the command. It should work the second time.

PS /home/user> Import-PSSession $Session -DisableNameChecking

This gives you commands like “Get-Mailbox” or “Set-MigrationBatch”.

An alternative way of connecting (Recommended).

There is actually another way of connecting to Exchange Online, but it was never a reliable way for me for some reason. Apparently, this is required for the “modern authentication” method. But still, let’s see how it’s done.

  1. Start Powershell.
fedora-kde :: ~ » pwsh

2. Install the “ExchangeOnlineManagement” Module.

PS /home/user> Install-Module –Name ExchangeOnlineManagement

3. Connect to Exchange Online.

PS /home/user> Connect-ExchangeOnline

This will open a tab in your browser, asking for your O365/M365 credentials. Once you input those, it will connect you in powershell.

Once that is done, you can input the same commands.

PS /home/user> Get-Mailbox

If you are using Firefox in combination with the “Multi-Account Containers” and want to connect to a specific profile use this command.

This will give you a URL with a code you need to input.

PS /home/user> Connect-ExchangeOnline -Device
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code DZDQYPENA to authenticate.

Now you can copy the URL and paste it into the Firefox container you want. I use this to connect to a specific customer.

If you don’t know about the “Multi-Account Container” add-in, than I would suggest to take a look at it. It’s very useful if you have to juggle multiple accounts for different customers for the same portal.

Links:

Leave a Reply