Freigeben über


HOW TO: Enable Mailboxes in different Storage Groups based on first character of First Name using Powershell script

In case you have already created the AD accounts and wants to enable mailbox and have a requirement that the mailboxes should be created in different Storage Groups based on the First character of the First Name, you can use the method shown below:

In this case the Storage Groups are named as follows:
A-B Users
C-D Users
E-F Users
G-H Users
....
The list of the users are available in a text file. Format of the text file is as below:

The First row denotes the Field Names.The file is named UserAccounts.txt
Account,First,Last
rjohn,Robin,John

Create a .PS1 file as below:

 #Gets the Server Name
$server = [System.Environment]::MachineName 

#Domain Name
$domain = "MyCompany" 

import-csv UserAccounts.txt |
    foreach{ 
        #Create the ID
        $id = $("$domain"+"\"+$_.Account);
        $alias = $_.Account 

        #Get the first character of the first name
        $str =$_.First.substring(0,1)
        $Database="" 

        #Set the Database based on the first character of the first name
        #You might need to change the name of the Storage Group and Database based on
        #your environment. 

        switch -regex ($str.ToLower()) 
         { 
                "[a-b]" {$Database = $server+"\A-B Users\"+"Mailbox Database"} 
                "[c-d]" {$Database = $server+"\C-D Users\"+"Mailbox Database"} 
                "[e-f]" {$Database = $server+"\E-F Users\"+"Mailbox Database"} 
                "[g-h]" {$Database = $server+"\G-H Users\"+"Mailbox Database"} 
                "[i-j]" {$Database = $server+"\I-J Users\"+"Mailbox Database"} 
                "[k-l]" {$Database = $server+"\K-L Users\"+"Mailbox Database"} 
                "[m-n]" {$Database = $server+"\M-N Users\"+"Mailbox Database"} 
                "[o-p]" {$Database = $server+"\O-P Users\"+"Mailbox Database"} 
                "[q-r]" {$Database = $server+"\Q-R Users\"+"Mailbox Database"} 
                "[s-t]" {$Database = $server+"\S-T Users\"+"Mailbox Database"} 
                "[u-v]" {$Database = $server+"\U-V Users\"+"Mailbox Database"} 
                "[w-x]" {$Database = $server+"\W-X Users\"+"Mailbox Database"} 
                "[y-z]" {$Database = $server+"\Y-Z Users\"+"Mailbox Database"}
                default {$Database = $server+"\First Storage Group\"+"Mailbox Database"}
         }
        # Run the actual command to enable the mailbox.
        enable-Mailbox -Identity $id -Database $Database -Alias $alias
        -DisplayName $($_.First+ " " + $_.Last)

  }

To Run the script:

1)Open the Exchange Management Shell

2)Navigate to the location where you have stored the Data and the script file.

3)Type in .\ScriptName.PS1 and hit enter to execute the script.

Comments

  • Anonymous
    October 06, 2008
    Thanks for the script. but when I ran the script with the example. It generates errors such as You cannot call a method on a null-valued expression. $str =$_.First.substring(<<0,1) enable-mailbox is not recognized as a comdlet etc enable-mailbox <<<< -Identity $id -Database $Database -Alias $alias Do you have any idea why this is the case?

  • Anonymous
    October 07, 2008
    Do you have the UserAccounts.txt in the same folder as the .PS1 script and is it in the same format as specified. The first row is the header row and the names of the columns have to be the same. The second row has the actual data.For the second error, are you running the script from the "Exchange Management Shell"?

  • Anonymous
    October 07, 2008
    Hi Akash,I figured out the problem. I hadAdd-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin on the top of the script and it works like a treat.Thanks for your help :D

  • Anonymous
    October 09, 2008
    Akash,It does not run on my cluster but it's ok on a standalone.Comes up withImport-Csv : Cannot open file C:UserAccounts.csv.At C:scriptsenable2.ps1:8 char:11import-csv  <<<< "c:UserAccounts.csv" |Do you know what's the problem?

  • Anonymous
    October 10, 2008
    Looks like you got the file name wrong. Is the file extension .txt or .csv?

  • Anonymous
    October 12, 2008
    I've tried both txt and csv extension.It still doesn't matter because with have a cluster name called i.e. clustname and when I tried to run the script on each exchange node it comes up with that message.

  • Anonymous
    October 13, 2008
    I've tried both txt and csv. It does not matter.Would it be permission issue? I have given the account full access to both the ps1 and txt. Also, run the ems as administrator but it does not matter.Strange?

  • Anonymous
    October 15, 2008
    Can you just try the import-csv "C:UserAccounts.txt" command. Try it from the EMS and the Windows Powershell console. I get that error only when the file name is wrong