Managing Exchange On-line with PowerShell

In my earlier articles on Office 365 and PowerShell, I explained broadly how PowerShell might be of use in managing your Office 365 subscription. As noted earlier, you can also use PowerShell to manage the Exchange part of your Exchange Online subscription that comes with Office 365.

PowerShell Remoting

Before getting into the details of PowerShell and Exchange Online, I'd like to take a second to explain some of the underlying technology Exchange Online uses for admin. With PowerShell remoting, you can create a session from your workstation on some remote servers. You can use that session to run cmdlets, and scripts on that remote system or systems. PowerShell scales well so you can create sessions on as many computers as you have in your environment (i.e. thousands of machines!)

If you create a remote session, you can export cmdlets form that remote session and import them into your local session. Thus you can load a module on your remote system (the remote Exchange Online server holding your Office 365 mailboxes) and import them into your local computer. Under the covers, PowerShell creates what are called proxy functions - these are functions on your local machine with the same name as the cmdlets. When you run the local function, the function will invoke the cmdlet remotely. This is incredibly powerful - you could create a session on a domain controller, a SharePoint server and a Lync Server and write scripts that combine cmdlets from the different servers. When you run the script it calls the local functions that then run the cmdlet remotely and return the cmdlet data. This is an incredibly powerful mechanism that offers great simplicity to the admin (i.e. YOU!).

This technique, which differs from how you administrate the Office 365 service (as we saw in Managing the Office 365 Service with PowerShell), is the approach used to administrate Exchange Online. There is currently no support for managing SharePoint Online or Lync Online. But when there is, I am guessing that they will use this remoting method. We'll see when Microsoft release cmdlets for these online servers.

Remoting with Office 365 and Exchange Online

Based on the above, the first step with Exchange Online is to create a remote session and import it locally. This is pretty straightforward - here's the code to do this:

# First Get credential for site

$cred365 = get-credential

# Next create a new PS Session

# Use a hash table to setup parameters to new-pssession

$Office365 = @{ ConfigurationName = "Microsoft.Exchange"

Connectionuri = "Https://ps.outlook.com/powershell"

Credential = $cred365

Authentication = "Basic"

AllowRedirection = $true}

$s = New-PsSession @Office365

This script first gets the credentials for the account. Next the script creates a hash table containing the parameters to be used on the call to New-PsSession. Then we use that hash table and open a remote session.

At this point, you could use the Enter-PsSession cmdlet, and open up a remote shell connection to the server (much like you might use Telnet to open a session on a remote Unix Server). Alternatively you can import the cmdlets as follows:

$results = Import-PsSession $s

This command has an interesting side effect - it creates a system-named module which is, in effect, imported. You can use get-module to see the name of the module. Once you have imported the session and created the dynamic module, you can use all of the cmdlets that Exchange Online exposes. For example, you could issue the following pipeline to get stats on your Exchange Online mailboxes:

# Get mail boxes and statistics

Get-Mailbox | Get-MailBoxStatistics

It's worth noting that you do not get access to ALL the Exchange 2010 cmdlets. As an Office 365 tenant, you cannot access everything. For example, you can't create a new mailbox database - that's a task the hoster (Exchange Online) does for you. In all, you get access to 234 out of the circa 600 Exchange 2010 cmdlets.

Assuming you have used the above script fragment, you can get the name of the module by just calling Get-Module, then use the dynamically named module name and call Get-Command using the dynamic module name. Bringing it all together Here's a more complete script and a screen shot of the output:

If you would like to save on typing and experiment with the script, you can copy and paste the version below but remember there are no warranties included :-)

# Office 365 connection script (Exchange365.ps1)

# Thomas Lee - tfl@reskit.onmicrosoft.com

# First Get credential for site from settings in the current PowerShell session

$cred = $cred365

# Next create a new PS Session

# Use a hash table to setup parameters to new-pssession

$Office365 = @{ ConfigurationName = "Microsoft.Exchange"

Connectionuri = "Https://ps.outlook.com/powershell"

Credential = $cred

Authentication = "Basic"

AllowRedirection = $true}

$s = New-PsSession @Office365

# Note: you will see the session being redirected to your site

# Next Import the remote session

$results = Import-PsSession $s

$results

# See the module name

Get-Module tmp*

# And away you go. Here are some things to try:

# How many cmdlets returned?

"{0} functions exported from Exchange ON-Line" –f $Results.ExportedFunctions.count

# Mail box stats:

Get-mailbox | Get-MailboxStatistics | ft -a

Summary

Using PowerShell's remoting feature it's quick and easy to create a connection to your Exchange 365 account. Once connected, you can administer all the client features of Exchange Online. If you are an enterprise user, you can use the proxy functions created by the remote session in conjunction with any other remote or local scripts/functions/cmdlets.