Steve Goodman's Exchange Blog
27Nov/100

Managing Office 365 and On-Premises Exchange 2010 from the same Powershell Session

I've just been reading on Mike Pfeiffer's blog this article about connecting Remote Powershell to Office 365. I've not yet got my beta account on Office 365, but do use Live@EDU/Outlook Live and had been wondering how similar administration is. It turns out that it's exactly the same (even down to the server names) therefore I thought it might be worth sharing a method I've been using for some…

Because there is such a big overlap of cmdlets between your On Premises Exchange 2010 environment and Office 365/Outlook Live, it can be a bit of a pain when you want to write a script that performs actions on both. I've documented how to do this in a previous post, but when you are disconnecting/connecting between environments, it can get pretty confusing. A simple error in a script can mean you create mailboxes in the wrong environment.

The solution is to use the -Prefix parameter when you're connecting to each environment. This means that, for example, Get-Mailbox can appear as Get-OnPremisesMailbox and Get-CloudMailbox. Your scripts can now easily target either environment, or both in the same script and you won't need to keep on checking whether you're performing actions against the local Exchange server or your "cloud" environment.

To demonstrate how simple this is, here's a quick example of connecting to both environments:

# Connect to On Premises Exchange
$OnPremisesSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.contoso.com/powershell/ -Authentication Kerberos
Import-PSSession $OnPremisesSession -Prefix OnPremises
# Connect to Office 365 / Outlook Live
$CloudCredential=Get-Credential
$CloudSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $CloudCredential -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue
Import-PSSession $CloudSession -Prefix Cloud

This should connect us to both environments and even allow us to combine On Premises and Cloud Powershell cmdlets together. For example, to get a total of all your mailboxes, both On Premises and in Office 365 / Outlook Live:

(Get-OnPremisesMailbox -ResultSize Unlimited).Count + (Get-CloudMailbox -ResultSize Unlimited).Count

You can also combine commands via the pipeline. In the next example, we will get all On Premises mail-enabled users that have an External Email Address (eg they are synced using OLSync/DirSync) in our Office 365 / Outlook Live domain, then start a foreach loop (using the % shorthand) and then retrieve details about the mailboxes from Office 365 / Outlook Live:

Get-OnPremisesMailUser -ResultSize Unlimited-Filter {ExternalEmailAddress -like "*@contoso.onmicrosoft.com"} | %{Get-CloudMailbox $_.UserPrincipalName}

Finally, a few notes for those who are just starting to play with Office 365 or Outlook Live via Remote Powershell. Firstly, you might need to set your Powershell execution policy (at an elevated command prompt) the first time you connect:

Set-ExecutionPolicy RemoteSigned

And if you are developing/testing and you find the Get-Credential part above tiresome, you can  replace the line with a hard-coded plain text username/password. Be wary of using this in your production environment, due to the security implications of hard-coding an admin password in clear text into a script:

$CloudCredential = New-Object System.Management.Automation.PSCredential "admin@contoso.onmicrosoft.com", (ConvertTo-SecureString "password" -AsPlainText -Force)

Hope this helps!

19Feb/101

Writing Powershell scripts that target Exchange 2007 and 2010

If you’re planning a migration to Exchange 2010 that won’t be completed overnight, you may want to be able to run Exchange Powershell scripts that execute both Exchange 2007 and 2010 cmdlets from the same script. There’s a few reasons for doing this, including:

  • If you use logic to determine where to place new user mailboxes, and some mailboxes will be on 2007 and some on 2010.
  • If you create new mailboxes on Exchange 2007 and then add the new mailbox to a Exchange 2010 distribution group (for example, if you use moderated distribution groups).
  • If you’re using Outlook Live (Live@EDU hosted Exchange 2010) and create MailUsers on-premise and Mailboxes in the hosted environment.
  • You are using the Transporter suite to migrate mail and need to create mailboxes on a temporary Exchange 2007 server, migrate mail, and then move mailboxes to Exchange 2010.

OK – so my examples are mostly about provisioning and these are real-life examples of where I’ve used this method, but I’m sure you can think of other examples where it might be useful to you. So – what do you need?

  • Exchange 2007 SP2 management tools on any OS that supports them.
  • Powershell 2.0
  • Remote Powershell enabled on the account you run the script as (using Set-User username –RemotePowerShellEnabled:$true)

You can use this on Windows Server 2003 R2, x86; no Exchange 2010 management tools are required. Enough of what you need – here’s what you need to put in your Powershell scripts to do Exchange 2007/2010:

# Add Exchange 2007. If you'll always run from the Exchange Management Shell, you don't need the next line.
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
# Exchange 2007 Commands Go Here - i.e.
Get-MailboxDatabase
# Unload Exchange 2007
Remove-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
# Create Remote Powershell session with Exchange 2010 - edit the server name in ConnetionUri
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.contoso.com/powershell/ -Authentication Kerberos
Import-PSSession $Session
# Exchange 2010 Commands Go Here - i.e.
Get-MailboxDatabase
# Unload Exchange 2010
Remove-PSSession $Session

If you’re looking to connect to Outlook Live instead of Exchange 2010, simply replace the line beginning “$Session =” with a couple of lines similar to this:

$LiveCred = New-Object System.Management.Automation.PSCredential "yourliveadmin@contoso.edu", (ConvertTo-SecureString "password" -AsPlainText -Force)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue

Hope this helps!

Steve