Back
Featured image of post Connect to Office 365 with PowerShell Core from non-Windows

Connect to Office 365 with PowerShell Core from non-Windows

As a Apple Fan boy, I’m happy that PowerShell Core is now available for macOS! Together with Visual Studio Code I’m able to run a lot of things on my Mac without having a virtual Windows machine running.

I was asked: “Do you use the native macOS PowerShell to connect to Office 365”. The short answer is: “Not really”!

And at the moment (December 2017) there are two main reasons for that:

  1. The connection is not fast and connection is not reliable! (See below)
  2. Many Office 365 services, like Skype for Business or Sharepoint, need a PowerShell local module and they are not available for non-Windows platforms yet!

I tested all the v6 beta releases and the basic Exchange Online connection worked in some, sometimes better, sometimes not. Even with the latest PowerShell Core v6 RC2 the connection isn’t always working or it is very slow during the connection or the import. Connecting to the Compliance Center seems to work like the Exchange Online connection: In general it seems to work… With exactly the same issues.

I tested everything on my Mac’s and on two CentOS based linux systems. And I found that is is good enough to do something quickly (e.g. change a Mailbox setting) but not good enough to use it as main Admin Shell or for Development (testing stuff). At least not yet!

Is is my environment:

PowerShell v6.0.0-rc.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /Users/josh> uname -a
Darwin HOMER 17.4.0 Darwin Kernel Version 17.4.0: Mon Dec 11 21:15:27 PST 2017; root:xnu-4570.40.9~4/RELEASE_X86_64 x86_64

How I try to connect:

$O365Credential = Get-Credential -Message 'Please enter your Office365 Credentials here' -UserName 'You@domain.tld'
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Credential -Authentication Basic -AllowRedirection -EnableNetworkAccess
Import-PSSession -Session $Session -AllowClobber

How this looks like:

PS /Users/josh> $O365Credential = Get-Credential -Message 'Please enter your Office365 Credentials here' -UserName 'You@domain.tld'

PowerShell credential request
Please enter your Office365 Credentials here
Password for user You@domain.tld: ************

PS /Users/josh> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Credential -Authentication Basic -AllowRedirection -EnableNetworkAccess

PS /Users/josh> Import-PSSession -Session $Session -AllowClobber
WARNING: The names of some imported commands from the module 'tmp_3gxutbmz.c53' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

PS /Users/josh> Get-Mailbox -Identity 'hochwald' | Select-Object Name,Alias

Name  Alias
----  -----
josh  hochwald

PS /Users/josh> Get-User -Identity 'hochwald' | Select-Object RecipientType

RecipientType
-------------
UserMailbox

PS /Users/josh> Get-MailboxStatistics -Identity 'hochwald' | Select-Object ItemCount,LastLogonTime

ItemCount LastLogonTime
--------- -------------
     4810 12/30/17 2:26:08 AM

PS /Users/josh> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-ItemProperty...}
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Credential, Get-ExecutionPolicy...}
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     1.2        PSReadLine                          {Get-PSReadlineKeyHandler, Get-PSReadlineOption, Remove-PSReadlineKeyHandler, Set-PSReadlineKeyHandler...}
Script     1.0        tmp_3gxutbmz.c53                    {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, Add-MailboxFolderPermission, Add-MailboxLocation...}

PS /Users/josh> Get-Module tmp_3gxutbmz.c53

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0        tmp_3gxutbmz.c53                    {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, Add-MailboxFolderPermission, Add-MailboxLocation...}

PS /Users/josh> (Get-Command -Module tmp_3gxutbmz.c53).Name.count
636

Problems: The New-PSSession commend hangs often!

# If the connection hangs
PS /Users/josh> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Credential -Authentication Basic -AllowRedirection -EnableNetworkAccess -Verbose

^C

Even after 5 minutes, nothing happend! Solution: crtl + c and try again. Most of the time, I had to open a new PowerShell Session, what could be annoying! Because you have to start from scratch…

The Import-PSSession throws an error!

# The import does NOT work all the time
PS /Users/josh> Import-PSSession -Session $EXSession -AllowClobber
Import-PSSession : Running the Get-FormatData command in a remote session reported the following error: Processing data from remote server outlook.office365.com failed with the following error message: ERROR_WSMAN_INVALID_SELECTORS: The WS-Management service cannot process the request because the request contained invalid selectors for the resource.  For more information, see the about_Remote_Troubleshooting Help topic..
At line:1 char:1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidResult: (:) [Import-PSSession], RuntimeException
+ FullyQualifiedErrorId : ErrorFromRemoteCommand,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Starting a new Shell often solved the problem for me!

The Performance is bad! It takes up to 2 (two) minutes until the connection is up and the import of the new session is done. And I found not solution for that.

I was told to use ‘https://outlook.office365.com/powershell-liveid/?proxymethod=rps_' instead of ‘https://outlook.office365.com/powershell-liveid/_'. That doesn’t really helped me. In my latest experiments i tried ‘-EnableNetworkAccess’ at the ‘New-PSSession’, but this also doesn’t solved my issues.

So, the long answer to the questing “is it working” will be: Sometimes is seems to work, often not. Would I recommend it right now? Definitely not yet! Whenever possible use a Windows Based system to connect to Office 365 right now!

PowerShell Remote is my favourite solution! Of this is not possible, I use a virtual Windows 10 based system with PowerShell 5.