How to use Task Scheduler for Exchange scripts

Configuration Steps

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.

  1. First test your script or Exchange commands from the Exchange Management Console. If it can't run there, it will certainly fail as a scheduled task.
  1. In Task Scheduler create a Basic Task

  1. Choose a schedule.

  1. Choose “Start a program” as the Action

  1. 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.

  1. 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>"

If I’m running the Get-EASlogs_scheduled.ps1 script from my scripts folder, the full arguments section needs the following syntax:

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange

Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; .'E:\Program Files\Microsoft\Exchange Server\V14\scripts\Get-EASlogs_scheduled.ps1'"

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 Scriptwith the path and name of the Exchange Management Shell script orthe Exchange commands to schedule.

  1. 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 some advanced configuration needed.

  1. 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)

  1. (Optional) If you need the script to run more frequently than daily, simply create multiple daily triggers varying the start time.
  1. 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"

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 is expected since the CentralAdmin toolset is only used by Office 365.

Check out “Behind the Curtain: How we run Exchange Online” for clues about this internal Central Admin tool (starting slide 27).

Acknowledgements

Much of this content was taken from Steve Goodman’s blog post on MSExchange.org