Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to simplify checking the dirty status of Exchange Mailbox databases.

Hey, Scripting Guy! Question Hey, Scripting Guy!, I have a large number of Exchange servers, and when I do a database restore, often I am unable to mount the database because it says that the database is dirty. So how can I use Windows PowerShell to fix and to mount these databases?

—SH

Hey, Scripting Guy! Answer Hello SH,

Microsoft Scripting Guy, Ed Wilson, is here. Well, it is nearly the weekend—it certainly does not quite seem like it should be nearly the weekend because the preceding days have flown by.

It's a wonderful shellIn one week—exactly one week—the Hey Scripting Guy! Blog hosts the “It’s a Wonderful Shell” holiday series written by Windows PowerShell MVP and Honorary Scripting Guy, Sean Kearney. You can see the teaser here.

SH, unfortunately, there are no cmdlets to obtain the state of an Exchange database. Therefore, you will need to use Eseutil. Microsoft PFE Mike Pfeiffer has a blog article where he talks about getting an Exchange database into a clean shutdown state by using the Eseutil.

Use Eseutil to verify the database state

Just because you need to use Eseutil to work with the Exchange database does not mean that you are completely out of luck when it comes to either Exchange or Windows PowerShell. This is because Windows PowerShell works really well with command-line utilities. In fact, Microsoft Windows PowerShell MVP Sean Kearney wrote an entire series of guest blog articles in which he talked about working with command line utilities from within Windows PowerShell.

One of the things I do not like about using Eseutil is that I have to provide it with the path to the database with which to work. I mean, the paths are always deeply nested, and then they seem to embed GUIDS and other confusing things that just make the path nearly impossible to type correctly. Luckily, I can solve this problem by using Windows PowerShell. The command shown here returns only the path to the Exchange database files.

Get-MailboxDatabase -Status | select edbfilepath

The command and its output associated are shown here.

Image of command output

Use PowerShell to mount or to dismount Exchange databases

Most of the time, when you need to use the Eseutil cmdlet to work with the status of an Exchange database, it is because the database will not mount. But if I am going to run Eseutil to check the dirty status of the database, then the mailbox databases need to be offline. This is easy to do using Windows PowerShell. All I need to do is use the Get-MailboxDatabase cmdlet to retrieve all of my Exchange databases, then I pipe the resulting objects to the Dismount-Database cmdlet, as shown here.

Get-MailboxDatabase | Dismount-Database –confirm:$false

When the above command runs, it will dismount all Exchange mailbox databases. It will not prompt—this is what the –confirm:$false parameter does.

If I want to mount all of the Exchange databases, then I pipe the resulting database objects to the Mount-Database cmdlet, as shown here.

Get-MailboxDatabase -Status | Mount-Database

Running the Eseutil command on each Exchange database

To find the status of the Exchange databases, I need to use the Eseutil command with the /mh parameters. I also need to specify the complete path to the Exchange Mailbox database. This is where Windows PowerShell shines. I have already seen where I can find the complete path to the Exchange Mailbox databases, and I also know that I can use Windows PowerShell to do repetitive operations. The % symbol in the command shown here is an alias for the Foreach-Object cmdlet. This means that for each mailbox database, I will run the Eseutil command on it, as shown here.

Get-MailboxDatabase -Status | % { eseutil /mh $_.edbfilepath }

The command produces extensive output. I can scroll through the output and pick out the State line. This is shown in the following image, where both the command and its output is shown.

Image of command output

If I do not want to worry with all of the output, I can easily filter the results by using the Select-String cmdlet. Now, of course, I can get really carried away with things, but hey, my Exchange Mailbox Databases are offline, and I want to know if they are clean or not. So, I use a really cheap Select-String pattern … I look for “State:” the command and associated output as shown here.

[PS] C:\>Get-MailboxDatabase -Status | % { eseutil /mh $_.edbfilepath } | Select-String -Pattern "State:"

            State: Clean Shutdown

            State: Clean Shutdown

[PS] C:\>

Bring the Mailbox databases back online

Ok, so now I know they are both in a clean shutdown state. So I should be able to bring them back online. I use the command shown here to bring them back online.

Get-MailboxDatabase –Status | Mount-Database

No information returns from the previous command, and therefore, I might want to ensure the mailbox databases actually came on line. To do this, I use the Get-MailboxDatabase cmdlet and select the name and the mounted properties, as shown here.

[PS] C:\>Get-MailboxDatabase -Status  | select name, mounted

Name                                                                         Mounted

----                                                                                  -------

Mailbox Database 1301642447                               True

Mailbox2                                                                        True

[PS] C:\>

SH, that is all there is to using Windows PowerShell and Eseutil. Join me tomorrow when I will talk about using the CIM cmdlets to query association classes.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy