Sharepoint Online Powershell Module on an Exchange Server

I have a massive Powershell script that performs end user service provisioning across a number of on-premises and Microsoft 365 services. Recently, I needed to add some code for setting security options for Sharepoint sites hosted on Sharepoint Online. Since the original purpose of this script was to provision and configure Exchange mailboxes, it runs within the Exchange Management Shell.

It turns out that this is a problem.

When attempting to load the Sharepoint Online PowerShell Module, I get this error:

[PS] C:\Windows\system32>Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Import-Module : Could not load type 'Microsoft.Online.SharePoint.TenantAdministration.SPOTenantCdnType' from assembly
'Microsoft.Online.SharePoint.Client.Tenant, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.
At line:1 char:1
+ Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], TypeLoadException
    + FullyQualifiedErrorId : System.TypeLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand

Here’s the error in text. I know this is ugly and hard to read, but I’m putting it here primarily for the search engines:

[PS] C:\Windows\system32> Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

Import-Module : Could not load type ‘Microsoft.Online.SharePoint.TenantAdministration.SPOTenantCdnType’ from assembly
‘Microsoft.Online.SharePoint.Client.Tenant, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’.
At line:1 char:1
+ Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChec …
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Import-Module], TypeLoadException

+ FullyQualifiedErrorId : System.TypeLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand

I confirmed that the Microsoft.Online.SharePoint.PowerShell is installed by running this cmdlet:

Get-Module -ListAvailable -Name “Microsoft.Online.SharePoint.PowerShell” | ft version, name

I found these two articles which seemed to offer promising solutions:

Their solutions didn’t work, unfortunately, but it did lead me to discover that Exchange Server includes a collection of SharePoint dlls.

So, I tried loading the Sharepoint Online module outside of the Exchange shell, and…

There it is.

The problem was a conflict with the SharePoint DLLs included with Exchange Server. You can load the SharePoint Online Powershell Module on an Exchange Server just fine as long as you don’t try to load it from within the Exchange Management Shell.

Leave a Reply

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