TitleRequired aka the Script-A-Tron

powershell2xa4Those of you following me on Twitter will no doubt have seen or ignored more than a few tweets from me recently about how much i like PowerShell.

Not least because it makes you look clever if you have a script open on one of your monitors.

I have said it before, Command line stuff just impresses people, its a scientific fact. Read more of this post

SBS 2011 Standard : Exchange 2010 Single Message Restore

sbsstdDuring a recent SMB MVP Community Roadshow event in Edinburgh, a member of the audience asked us about Email restore in SBS 2011.

Although i knew this could be done, i had never actually done it. So i decided to work out how it was done with SBS 2011.

I found a lot of great resources online that helped me with the process and i have linked to them at the end of this post.

I’m focusing on a situation where we are restoring from a backup, rather than what is available through the Exchange dumpster (aka Recover Deleted Items) although i do cover that briefly below.

Read more of this post

Exporting Mailboxes to PST (ExMerge) on SBS 2011 – Scheduled Task Part 2

Now that we have worked out our syntax for our powershell scripts, we need to add them to some scheduled tasks to run them out of normal business hours – or just to automate them so you don’t have to do it manually.

In Part 1 we covered which commands we are going to run if you missed that, you can find it here.

You will need to have these commands saved as powershell scripts, (.ps1 files) you can do that by simply putting the syntax into notepad and saving the file with the .ps1 extension.

The 3 scripts are:

$Export = get-mailbox; $Export|%{$_|New-MailboxExportRequest -FilePath \\localhost\PST\$($_.alias).pst} |select-object filepath, sourcedatabase, mailbox, status | format-list| out-file c:\users\admin\mailbox-export-log.log

get-mailboxexportrequest | select-object filepath, sourcedatabase, mailbox, status | fl |  out-file c:\users\admin\mailbox-export-result-log.log

get-mailboxrequest -status completed | remove-mailboxrequest -confirm:$all

get-mailboxexportrequest -status completed | remove-mailboxexportrequest -confirm:$all

To add them to a scheduled task is slightly different than running your average batch file, it even has a little more complexity than running a normal powershell script, because for exchange cmdlets to run, you have to load the exchange powershell snapin as part of your scheduled task.

You can find a lot of useful info out on the net, some of the most useful posts i found are linked at the end of this article.

Firstly you will want to add a new scheduled task,  if you go to Start > All Programs > Accessories > System Tools > Task Scheduler

On the top right, you can click ‘Create Task’


You will need to enter a name for the task, i would also suggest enter in some details here to document what the task does. You will also need to click, ‘Run Whether user is logged on or not’ and ‘Run with Highest Privileges’


Switch to the ‘Triggers’ tab, and click on New. Here you can define the schedule that the task will run to, i would suggest you gauge the length of time the task takes to run, so you know how much time you need to run the task to minimise any disruption or performance issues.


Click on OK. You can see your trigger defined. Now click on the ‘Actions’ tab and click ‘New’.


In the program/script field enter the following:

PowerShell.exe -command ". ‘c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; & C:\users\dfunk\documents\scripts\export-all-mailboxes.ps1"

This breaks down to the following (based on my interpretation)

PowerShell.exe (runs powershell)

-command ". ‘c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’;

(launches the exchange shell component)

Connect-ExchangeServer -auto; (connects to the exchange server)

& C:\users\dfunk\documents\scripts\export-all-mailboxes.ps1" (executes our script)


Click on OK to save this syntax.


You are prompted because the task scheduler detects arguments to run with your command, click yes here.

Click on New again, to add a second action, which is to send an email.

Choose ‘Send an e-mail’ from the dropdown menu.

Fill out the details that are relevant to your environment.


Click on OK, and then click on OK again to save your task.

You will be prompted for your password as we are allowing this task to run if a user is logged on or not.


Enter your password and click ok, and you be returned to the task scheduler. You will see your new task added.

Right click the task, and choose ‘Run’ to test your task.

If you click on your task, and go to the History tab, you will see the status of your task as it runs…


You can see both actions have completed.

Now, let’s check we have a log file created..


And we should also have an email…


Not a particularly inspiring email it has to be said, but it does tell us that our task has kicked off.

Now, we can add a second scheduled task to run our second powershell script. This script will be almost identical but we will be running our powershell script to check on the status of the exports.

With this task you can also add an attachment to the email action, which is the output log file of the powershell script.

get-mailboxexportrequest | select-object filepath, sourcedatabase, mailbox, status | fl |  out-file c:\users\admin\mailbox-export-result-log.log

This command will output the status of the exports to the c:\users\admin\mailbox-export-result-log.log.

Depending on the length of time it takes for the script to execute you may need to separate the ’email log action’ into a third task, so you actually capture the log file after the export results are complete. Otherwise the log file may show that the mailboxes are either ‘queued’ or ‘in progress’


Your email should look something like this..


Now, the final task to add, is our final script.

get-mailboxrequest -status completed | remove-mailboxrequest -confirm:$all

get-mailboxexportrequest -status completed | remove-mailboxexportrequest -confirm:$all

This should be run as the final script, after your results email has arrived to clear the logs for the next time the tasks are run.

I used the following links to pull this info together as well as the built-in powershell help.





Updated 10/2/2012 Fixed Syntax for Export Request

Exporting Mailboxes to PST (ExMerge) on SBS 2011 – Scheduled Task Part 1

So it looks like Wayne and Gagan beat me to posting the steps to exporting mailboxes out to PST files, the same functionality we used ExMerge for in the 2003 era, however something they didn’t publish was how to schedule it.

So before they do, i thought i would have a go.

Firstly to make it quicker for me to publish and increase the likelihood of beating them to it, i will assume you have read their awesome guide here..

So following on from them, let’s assume you want to schedule this to run each night, or weekly as a compliment to your already ‘robust’ backup routine, which i am sure is the corner stone of your foolproof disaster recovery plan.

You do have a plan don’t you?

Let’s examine the commands we need to run.

$Export = get-mailbox; $Export|%{$_|New-MailboxExportRequest -FilePath \\localhost\PST\$($_.alias).pst}

This command will execute to export all mailboxes on the server, and save them to a share called PST, inside which the mailboxes will be named alias.pst, from the screen shot below you can see my 3 mailboxes..


Now we are going to be scheduling this, so we wont be running it interactively, but we will want a log of what has occurred right?

so let’s append some text to our powershell command..

$Export = get-mailbox; $Export|%{$_|New-MailboxExportRequest -FilePath \\localhost\PST\$($_.alias).pst} | out-file c:\users\admin\mailbox-export-log.log


After a few seconds we are returned to a prompt..

Let’s open up the resulting file and see what we have got.


The resulting output is not really very friendly for a log file – yes we can see the exports are queued and we dont have any errors but it might be better if we could format the output a little neater.

If we change the syntax again slightly to include ‘format-list’ (fl for short) the output changes greatly.

$Export = get-mailbox; $Export|%{$_|New-MailboxExportRequest -FilePath \\localhost\PST\$($_.alias).pst} | format-list | out-file c:\users\admin\mailbox-export-log.log


Now this is probably too much information to review for a log file, so we now might want to narrow down what we are going to report.

Let’s now add some more syntax to our command, this goes in before we put the ‘format-list’ command or Fl for short.

$Export = get-mailbox; $Export|%{$_|New-MailboxExportRequest -FilePath \\localhost\PST\$($_.alias).pst} | select-object filepath, sourcedatabase, mailbox, status | format-list | out-file c:\users\admin\mailbox-export-log.log


This is much more manageable – we can see the path to the exported PST, we can see the source database (if we need it, if you are running more than one database) the actual mailbox and the request’s status.

You can choose which fields to include by looking in the left hand collum in the screen shots above to find the correct property name, then just include them in the syntax, for example to add the request guid and request name…

select-object filepath, sourcedatabase, mailbox, status, request guid, name  |

What we can do now is run a second command to pull our results into a second log file.

get-mailboxexportrequest | select-object filepath, sourcedatabase, mailbox, status | fl |  out-file c:\users\admin\mailbox-export-result-log.log


Hmm hold on – this is showing me EVERY mailbox export request ever. This is not ideal because our log file is going to be huge.

So what can we do?

As written in Wayne and Gagan’s article we can run this command to clean things up..

Get-MailboxExportRequest -Status Completed | Remove-MailboxExportRequest


If i press A to get rid of all the reports i am sent back to a PS prompt.

Now if i run my report again, i should get an empty log, because there are no mailbox export requests left on the system.

get-mailboxexportrequest | select-object filepath, sourcedatabase, mailbox, status | fl |  out-file c:\users\admin\mailbox-export-result-log.log


So, the next time we run our export this log will be populated only with those particular export statistics, and in turn can be cleaned out by running the remove command.

In the next part of this article i will show you how to turn this into a scheduled task.

Part 2

%d bloggers like this: