Using the new V2 ExO Powershell Module in a script (and the old way if you want!)

As you know, Microsoft announced the new Exchange Online REST-based Powershell commands at Ignite: https://techcommunity.microsoft.com/t5/Exchange-Team-Blog/Exchange-Admin-Improvements-Announced-at-Microsoft-Ignite-2019/bc-p/985019#M27379

Loading the modules from a script is similar to loading the V1 version, passing the credential with $UserCredential in this example.

Import-Module $(Get-ChildItem -Path $("C:\Jobs\ExchangeOnlineManagement\") -Filter Microsoft.Exchange.Management.RestApiClient.dll -Recurse ).FullName
Import-Module $(Get-ChildItem -Path $("C:\Jobs\ExchangeOnlineManagement\") -Filter Microsoft.Exchange.Management.ExoPowershellGalleryModule.dll -Recurse ).FullName
$EXOSession = New-ExoPSSession -Credential $UserCredential
Import-PSSession $EXOSession

With the new version, you load two dlls: The API dll, followed the GalleryModule in that order.

Note that the path is an arbitrary one in this example. I simply copied over the module to that directory. By default, the V2 version will be installed into the \Users\\Documents\WindowsPowerShell\Modules\ExchangeOnlineManagement directory and you can reference that path instead.

Another thing to mention. From what I have seen, if you load the the trusty Azure MSOnline (Connect-MsolService) module in a scheduled task script after you load the V2 Exo module, it will fail, so load it before the V2 stuff. Maybe that will get fixed in a future version.

As always, disable any MFA for the service account you are using for this process. Ths new module leverages Modern Auth, just not MFA- same as the current V1 ExO friendly module.

If you are inclined to use the V1 version still, you can always use these tried and true methods:

$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+”\Apps\2.0\”) -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) . “$MFAExchangeModule” Connect-EXOPSSession -Credential $Credential

or

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+”\Apps\2.0\”) -Filter http://Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch “_none_”}|select -First 1) $EXOSession = New-ExoPSSession -Credential $Credential Import-PSSession $EXOSession

Consider using Conditional Access for these special accounts and only allow access from trusted IPs

Published by adavid6

Grumpy Old Exchange MVP. My old site was lost: https://web.archive.org/web/20180307110652/http://no-one-uses-email-anymore.com/ I have been an Exchange MVP since 2002. I also had the honor of naming “You had me at EHLO” for the Exchange Product Group Blog way back in the early 2000s. I attempt to answer forum questions on TechNet: https://social.technet.microsoft.com/profile/andy%20david/ and can be found on Twitter: https://twitter.com/adavid6 Reddit: https://www.reddit.com/user/adavid1608 Microsoft Tech Community: https://techcommunity.microsoft.com/t5/user/viewprofilepage/user-id/86

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: