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’

9

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’

10

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.

13

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

15

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)

25

Click on OK to save this syntax.

26

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.

27

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.

21

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…

28

You can see both actions have completed.

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

29

And we should also have an email…

30

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’

31

Your email should look something like this..

32

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.

http://technet.microsoft.com/en-us/library/bb123798.aspx

http://www.mikepfeiffer.net/2010/02/creating-scheduled-tasks-for-exchange-2010-powershell-scripts/

http://computerperformance.co.uk/powershell/powershell_format_list.htm

http://blogs.technet.com/b/sbs/archive/2011/05/09/how-to-import-and-export-mailboxes-using-pst-files-in-sbs-2011-standard.aspx

Updated 10/2/2012 Fixed Syntax for Export Request

About Robert Pearman
Robert Pearman is a UK based IT worker bee. He has been working within the IT Industry for what feels like forever. Robert likes Piña colada and getting caught in the rain, he also enjoys writing about Technology like PowerShell or System Automation but not as much as he used to. If you're in trouble, and you can find him, maybe you can ask him a question.

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

  1. Geoff says:

    Thank you for the nicely detailed article. A few things didn’t work for us:

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

    the -confirm:$all didn’t work for us and we changed it to -confirm:$false

    http://social.technet.microsoft.com/Forums/en/ITCG/thread/bddbca10-9245-4cec-b988-02e5f0f91515

    2) The command to execute our scripts in task scheduler also didn’t work:

    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″

    We use the following (on both Exchange 2007 and 2010):
    PowerShell.exe -PSConsoleFile “C:\Program Files\Microsoft\Exchange Server\V14\Bin\ExShell.Psc1” -Command “. ‘C:\users\dfunk\documents\scripts\export-all-mailboxes.ps1′”

    *Note there is a single quote closing the export-all-mailboxes.ps1 script and a double quote closing off the section after -Command

    Hope this helps some folks.

    • Hi Geoff,

      Thanks for the corrections.

      I’m sure I copied and pasted what I typed in, however it is possible some errors have cropped in.

      To be honest looking back at this code is pretty poor, so I think I will update this with some better powershell.

      Keep an eye out for a new post.

Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.