<

How to set an Exchange 2007 Mailbox Database description using PowerShell and ADSI

Published on
10,120 Points
4,020 Views
1 Endorsement
Last Modified:
Approved

Why would you set a description on an Exchange 2007 mailbox database?

There are many reasons to set a description on a mailbox database, but the one most common is for scripting purposes. For instance, let's say that I have three different mailbox size categories. The first one is under 500MB, the second is under 1GB, and the last one is over 1GB. Now, lets also say that I have 9 different databases on the Exchage server, 3 for each size category.

I want to load balance my users on a nightly basis so that the users are in the appropriate store based on the size of their mailbox. I can hard code a script to put the users on specific databases which doesn't make the script very portable, or I can have it go to list of databases based on the database description and then pick the one with the most available storage.


So how do I set the description?

Well, first, I would decide on what you want the descriptions to be. To keep it simple, I will use A, B, and C. A for the under 500MB mailboxes, B for the under 1GB mailboxes, and C for the over 1GB mailboxes. So when I say it is a type B mailbox, then I know it needs to be in one of the Type B databases. Second, you need to decide which of the databases is going to be which type. In my case, I will round robin the databases. In other words, I will get a list of all databases, and then label them in order, A then B then C then A then B then C, so on and so forth. The names of the databases do not actually matter since all we will care about going forward is the description. This also makes it easier to code since I don't have to know the names of the databases. With that said, there is one exception. I do not want to label any Recovery Storage Group databases, so I will exclude them from the list (mine all start with RSG in the database storage group name). To get the list, use the following code:
#replace ServerName with the name of the Exchange Server
#write the list of database to an array
$mbdb = get-mailboxdatabase -Server ServerName| Where {($_.StorageGroupName -notlike "RSG*")}
#Sort the list of databases
$mbdb = $mbdb | Sort-Object

Open in new window

This will write out an array of databases on the specified server to the $mbdb variable.

Next, we will want to zero out some counter variables which will be used for the round robin approach.
$x = 0
$i = 0

Open in new window

We will use the $i counter to count how many database are in the array and then increment through them one at a time.
for ($i = 0; $i -le ($mbdb.Length -1); $i +=1)

Open in new window

Now for the meat of the code. From this point, you will increment the other counter ($x). This will be the revolving counter that determines which description to give the database. When it gets to 3, you will see that after it sets the description, it will set the counter back to 0 for the next round.

We will then grab the distinguished name of the current database. Once we have that, we check which counter ($x) we are currently on, and use it to set the appropriate description. If the value of $x is 1 we will set the description to A. If it is 2, then we will set it to B, and finally if it is 3, we will set it to C and then reset the counter to 0.
$x += 1
	$DN = $mbdb[$i].DistinguishedName
	Switch ($x)
	{
		1 {
			$Desc = "A"
		}
		2 {
			$Desc = "B"
		}
		3 {
			$Desc = "C"
			$x = 0
		}
	}

Open in new window

Once we have the description, we will then set it using ADSI. To do this we will create an LDAP connection to the distinguished name of the database and write the returned object to an array called $addb. This will have all of the AD attributes in it. We only care about the description attribute so we will set it to the description our code has selected for us and then save it using the SetInfo() parameter. It looks like this:
$addb = [adsi]"LDAP://$DN"
$addb.Description = $Desc
$addb.SetInfo()

Open in new window

That's it.

The whole code looks as follows:
$mbdb = get-mailboxdatabase -Server ServerName| Where {($_.StorageGroupName -notlike "RSG*")}
$mbdb = $mbdb | Sort-Object
$x = 0
$i = 0
for ($i = 0; $i -le ($mbdb.Length -1); $i +=1)
{
	$x += 1
	$DN = $mbdb[$i].DistinguishedName
	Switch ($x)
	{
		1 {
			$Desc = "A"
		}
		2 {
			$Desc = "B"
		}
		3 {
			$Desc = "C"
			$x = 0
		}
	}
	$addb = [adsi]"LDAP://$DN"
	$addb.Description = $Desc
	$addb.SetInfo()
}

Open in new window

How to move the mailboxes to the appropriate stores based on their size and the store's description will be shown in another article.
1
Comment
Author:GusGallows
0 Comments

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Join & Write a Comment

Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month