Tuesday, November 16, 2010

Exchange script in Task Scheduler

Want to run an Exchange script on a schedule?  For example, the script I posted to remove all items in the Deleted Items folder that are more than 30 days old.  If you are running an Exchange CCR, this should be done on a separate role from the mailbox nodes.

First you will have to add a line to the script so that it will know which domain controller to use.  Add these lines below and replace FQDN with that of your own domain controller.


#Sets the Exchange Server to use a specific domain controller.
Set-ExchangeServer -DomainController FQDN


In Windows Server 2008, open the Task Scheduler and create a new task.
Give it a Name and Description
Set the user to run under which should probably be one with Exchange Admin rights.
Check Run whether user is logged on or not
Set your schedule on the Triggers tab such as when you want it to run.
For the Action select Start a Program

I created a batch file with this line.  This will call Powershell with the Exchange extensions and then feed in the script we created for deleting items.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"; ". 'C:\Scripts\DeletedItems30Days.ps1'"
Program/script: Powershell.exe
Add arguments: -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\ExShell.Psc1" -Command ". 'C:\Scripts\DeletedItems30Days.ps1'"

Set any Conditions and Settings and press OK.

I get a nice little email every Friday evening with a list of all accounts, the number of items deleted and the amount of space removed from each account, then totals of each at the bottom.

No comments:

Post a Comment