|
Mailbox-enabling many users with the Exchange 2007 management shell (PowerShell) |
|
|
As you might have seen from some of my previous posts I’m doing some work with the ILM “2” beta. One of the pre-requisites for ILM “2” is Exchange 2007, so one of my test labs happens to consist of a 64-bit Windows Server 2003 R2 domain controller running Exchange 2007 SP1 (yeah, I know this isn’t really good practice, but this is a lab) and a 64-bit Windows Server 2008 running ILM and all its pre-requisite bits. Anyway, I created some sample users in the AD using good old CSVDE (as I wanted to use the names of characters from novels I’ve read recently) and needed to mailbox-enable these users. At this point I was running the RTM version of Exchange and there doesn’t appear to be a way to mailbox enable many users at once using the UI, so I had to drop into PowerShell (which I’ve been avoiding learning) and do this. Here’s how I did it.
Get-User -organizationalUnit "Organisational People" | Where-Object{$_.RecipientType -eq "user"} | Enable-Mailbox -Database "x64e12dc\Mailbox Database" | Get-Mailbox | Select-Object DisplayName,WindowsEmailAddress,Database
I’ll break this down to provide some additional explanation. But before I do that, let me show you the output of running this command on said OU. For the purpose of this example I’ve created a new OU called People and created three users in this OU. Running the above set of commands mailbox-enables these users and then outputs some basic information illustrating that this has succeeded.
[PS] C:\Users\paulw-a>Get-User -organizationalUnit "Organisational People" | Where-Object{$_.RecipientType -eq "user"} | Enable-Mailbox -Database "x64e12dc\Mailbox Database" | Get-Mailbox | Select-Object DisplayName,WindowsEmailAddress,Database
DisplayName WindowsEmailAddress Database ---- ------------------- -------- Oliver Williams olliw @ ilmv2.idmgmt.int X64E12DC\First Storage Group\... Jeff Page jeffp @ ilmv2.idmgmt.int X64E12DC\First Storage Group\... Tierloch Curtis tierc @ ilmv2.idmgmt.int X64E12DC\First Storage Group\...
[PS] C:\Users\paulw-a>
Right, so let’s explain how this works. Get-User is used to retrieve all user objects in the forest that match a specific set of criteria. In the case of the above example, that criteria is any non-mailbox-enabled user objects in an OU called people. We specify the first piece of criteria in the Get-User command with the OrganizationalUnit switch. The second piece of criteria, non-mailbox-enabled users, is defined using the Where-Object cmdlet. We differentiate between mailbox-enabled users and users with the RecipientType property of the Microsoft.Exchange.Data.Directory.Management.User class. If a user is mailbox-enabled then the value of this property is “UserMailbox” whereas for a regular [non-mailbox-enabled] user this is simply “User”. The following example illustrates this:
[PS] C:\Users\paulw-a>Get-User -organizationalUnit "People" | Select-Object DisplayName,WindowsEmailAddress,RecipientType
DisplayName WindowsEmailAddress RecipientType ----------- ------------------- ------------- Oliver Williams olliw @ ilmv2.idmgmt.int UserMailbox Jeff Page jeffp @ ilmv2.idmgmt.int UserMailbox Tierloch Curtis tierc @ ilmv2.idmgmt.int UserMailbox Neil Williams User
[PS] C:\Users\paulw-a>
What I’ve done here is run Get-User and piped the resultant objects to Select-Object. Notice that a fourth user object that I’ve created in this OU shows with a RecipientType of "User" and no WindowsEmailAddress.
OK, so once we’ve found our filtered set of users we pass the resultant objects to the Enable-Mailbox cmdlet. This cmdlet is used to mailbox-enable an existing user or inetOrgPerson object. As we’re passing the Microsoft.Exchange.Data.Directory.Management.User object to this cmdlet all we need to specify is the mailbox database store, which is specified with the Database parameter.
To finish we pipe the newly created mailbox to the Get-Mailbox cmdlet and then pipe that to the Select-Object cmdlet to output some basic information –in this case, the DisplayName, WindowsEmailAddress and Database properties of the Microsoft.Exchange.Data.Directory.Management.Mailbox class.
Note. Each PowerShell command is one single line with no breaks. I've added line breaks for readability and formatting purposes only. I've also added a space before and after the @ symbol in the output of each e-mail address to stop my blog from turning it into a "mail-to" command and breaking the line.
Hope this helps someone! |