Running Exchange Scripts as a Scheduled Task

Quite often I run into times when I want to automate a task in Exchange.  I usually use Task Scheduler to schedule any PowerShell script I want to run, but it can sometimes be a bit tricky.  There are many ways to do this and here is a couple of ways which work well for me.

I’m assuming you’ve got your Exchange script written and have tested it running from the Exchange Management Shell.  If it has issues running in the Exchange Management Shell, fix those issues first before you schedule the script.

In Task Scheduled, Right click and Create a New Task.

schtask1
Enter details in the name and description to match your script and ensure you have “Run wheather user is logged on or not” is selected and “Run with highest privileges” is ticked.

schtask2

Go to the Triggers Tab and Click the New Button.

schtask3

In here, we can setup a trigger based on a scheduled time.  There are other triggers available as well, but we’re just scheduling the script to be run at a certain time so we’ll be sticking to the trigger “On a schedule”.

schtask4

Fill in the time/s you want to run the script and press OK

Note: You create multiple triggers in case you want to run you script more frequently or on different schedules changing the time the script executes.
schtask5

Go to the Actions Tab and Click the New Button

schtask6

In this area we will enter the details to run the script.

During my testing, I’ve found two ways which this can be done.

The 1st method below is more convenient, but it does spawn additional PowerShell processes.  If you schedule a lot of PowerShell scripts you may want to take this into account for any overheads on the host you’re running the scripts on.

The 2nd method runs scripts with the Exchange environment, but it’s usually necessary to have extra PowerShell in your script to connect to Exchange.

Both methods assume that you’re script will run in PowerShell versions 1-4 and that you know the version and install path of Exchange on your the host running this script.

1st Method

In the Program/script: text box we need to enter the full path to PowerShell on this host.

By default, it is:

You can check this is correct by pressing the Browse button and selecting it.

In the Add arguments (optional): text box we need to enter arguments specific to your script.

For example, this is what would be used for Exchange 2013 installed on Drive C.

If you are using Exchange 2010, it would be slightly different.

Note: in the above examples, you’ll need to replace C:\Scripts\MyScript.ps1 with the full path to where the script is stored on this host.

Once you’ve entered this, press OK

2nd Method

In the Program/script: text box we need to enter the full path to PowerShell on this host.

By default, it is:

You can check this is correct by pressing the Browse button and selecting it.

In the Add arguments (optional): text box we need to enter arguments specific to your script.

For example, this is what would be used for Exchange 2013 installed on Drive C.  If you are using Exchange 2010, you’ll need to update the path to the PSConsoleFile to the Exchange 2010 version.

 

Note: in the above examples, you’ll need to replace C:\Scripts\MyScript.ps1 with the full path to where the script is stored on this host.

Once you’ve entered this, press OK

The press OK again to create the scheduled task.

When you do this, you will be prompted for credentials.  Make sure that you use credentials which have sufficient Exchange privileges to run the cmdlets used in your script.

Note: Consider running scripts from a server or workstation with the Exchange Management Tools installed which is not providing Exchange services (Not an Exchange server).  This will help protect the security of your Exchange servers by not performing tasks as the local administrator.

Tips

Tip #1

Check the installation path for Exchange on the host.  If it was installed on Drive D, the arguments in the scheduled task would look like this.

Tip #2

Use logging in your scripts and don’t rely on the scheduled task logs.  The scheduled task logs simply tell you if task started/completed properly and don’t tell you anything about how your script ran.  Using transcript in your script can be a quick way to add logging. You just add

to the start of your script and

to the end of your script.

Check out more info about start-transcript here.