Search code examples
powershellscriptingautomationexchange-serverhybrid

Powershell scripting: switching contexts between hybrid Exchange on-prem and online


Environment is hybrid Exchange with AD sync and ADFS/SSO.

I have a new user onboarding script that I'm writing that needs to perform commands against our on-prem server to create AD account and enable remote mailbox. It triggers an ADsync, waits 5 minutes, then connects to MSOLservice to provision a licence.

The next step is to set shared mailbox permissions, but when I run get-mailbox it's pulling on-prem mailboxes instead of remote ones.

I've tried unloading the EMS snap-in and AD module, and closing my sessions. As soon as I load get-mailbox it creates a session back to on prem.

Can't pipe get-remotemailbox into get-mailboxpermissions. Can't use invoke-command to run get-mailbox |get-mailboxpermission | where-object {$_.user -eq "user@domain.com"} (See no-language error here for why)


Solution

  • the solution is to use prefixes:

    http://www.stevieg.org/2010/11/managing-office-365-on-premises-exchange-2010-powershell-session/

    Import-PSSession $o365Session -prefix cloud
    get-cloudmailbox |get-cloudmailboxpermission ...