How to use Task Scheduler for Exchange scripts
Often times as an Exchange Administrator, there are opportunities to automate tasks. However, getting Exchange related scripts to successfully launch from Task Scheduler can prove challenging. This article will help navigate the typical hurdles involved.
- First test your script or Exchange commands from the Exchange Management Shell. If it can't run there, it will certainly fail as a scheduled task.
In Task Scheduler create a Basic Task
Choose a schedule
Choose "Start a program" as the Action
Add the following syntax to add for the Program/script text entry box
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Verification of the Powershell path can be made by selecting the Browse button. This is the default path however for Powershell v1 - v4.
6. Add the following syntax, depending on the version of the Exchange Server to schedule the task on in the "Add arguments" text box.
Here’s the example for Exchange 2010
-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <Your Script>"
Here's the example for Exchange 2013
-NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <Your Script>"
You'll need to substitute the path to the Exchange Server installation directory if you've changed if from the defaults, and replace <Your Script> with the path and name of the Exchange Management Shell script or the Exchange commands to schedule.
7. At the finish of the Basic Task Wizard, select the option to "Open the Properties dialog for this task when I click Finish" to configure additional required settings.
At the general tab, specify "Run whether user is logged on or not" (this is critical for RBAC) and "Run with the highest privileges" (this is necessary for User Account Control)
(Optional) If you need the script to run more frequently than daily, simply create multiple daily triggers varying the start time.
Saving the Task will prompt for credentials. Be sure to use an account with the necessary RBAC role to run the Exchange commandlets or the script. For security purposes, use the least privileged account / role that is able to launch the script or commands manually from the server the task is scheduled on. Consider scheduling the task to run from a workstation that has the Exchange Management Shell tools installed to prevent local administrator access on the Exchange servers. For more information see the Exchange Team blog on "Protecting Against Rogue Administrators"
http://blogs.technet.com/b/exchange/archive/2014/09/12/protecting-against-rogue-administrators.aspx
Troubleshooting and Verification
If the commands or script have already been verified from the Exchange Management Shell, the next step is to verify the commands or script can run from the RemoteExchange.ps1 environment using an elevated command prompt.
Here's an example for an Exchange 2013 command where Exchange is installed in the default location on C: and the output is piped to a local file.
C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -noExit -Command ".'C:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-queue > c:\temp\queue.txt"
Here's an example for an Exchange 2010 script with Exchange installed on E:
C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -noExit -Command ".'E:\Program Files\Microsoft\Exchange Server\V14\Bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; .'E:\Program Files\Microsoft\Exchange Server\V14\scripts\CollectOverMetrics.ps1' -DatabaseAvailabilityGroup RedDag1"
The Scheduled Task can then be verified further by right-clicking the task and choosing Run:
Additional troubleshooting can be found in the crimson channel logging. You can access this directly from the event log, or check the history tab of the scheduled task.
Microsoft-Windows-TaskScheduler/Operational
It’s a good idea to use scripts that have their own error logging. If the task scheduler reports a result code of 0, that means the task ran successfully. However, if the expected result of the script wasn’t realized, there isn’t a way to tell what went wrong without error logging initiated from the script.
Depending on how the script captures error logging, you may see errors related to running Exchange’s RemoteExchange.ps1
The following error seems to be expected.
Get-ItemProperty : Cannot find path 'HKLM:\Software\microsoft\ExchangeServer\v14\CentralAdmin' because it does not exist.
At C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1:107 char:21
+ if (Get-ItemProperty <<<< HKLM:\Software\microsoft\ExchangeServer\v14\CentralAdmin -ea silentlycontinue)
+ CategoryInfo : ObjectNotFound: (HKLM:\Software\..14\CentralAdmin:String) [Get-ItemProperty], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
ACKNOWLEDGEMENTS
much of this content was taken from Steve Goodman’s blog post on msexchange.org