Job Scheduling for Exchange 2013

As is the case for a lot of my blog articles, I tend to get distracted by one project or post which leads me to something completely unexpected. In this post I will cover a script I wrote to schedule a task for a test lab. The reason I wrote the script was to over one of the subsections of my lab script. I broke this piece out as I could see other potential uses for the script My scheduled task was mail flow for testing purposes.

Script Genesis
The script is a basic PowerShell scheduler for PowerShell scripts. Various information is populated – when to run, how often to run, what script to run, etc. What makes it useful is the ability to create a new PowerShell script for a repeatable process and then use the script for scheduling it out. Instead of going through the process of opening the Task Scheduler and setting those particulars up, this script is run once for each script that needs to be scheduled. It essentially does the hard work for you.

The Script Code
While working on the syntax of the script, I ran into some interesting terminaology or conventions that Microsoft uses in this particular cmdlets.

$frequency = read-host "How often do you want the emails to be sent [hourly, daily, weekly]"
$time = read-host "At what time should the task run (not hourly) [ie 12:00AM, 4:00PM]"
$filepath = read-host "filepath of the PS1 file to be run"
$name = read-host "Name of the scheduled job in PowerShell"
$range = read-host "Do you want this task to (1) run indefinately or (2) run for a period of time [1 or 2]"
if ($range -eq "2") {
	$start = read-host "Enter a start date [1/1/2015]"
	$stop = read-host "enter a stop date [4/1/15]"
    $duration = new-timespan -start $start -end $stop
} else {
    $duration = "([TimeSpan]::MaxValue)"

# Get days of the week for weekly task
if ($frequency -eq "weekly") {
    $weeks = read-host "Trigger the script every how many weeks [1 or 2 or 3 or...]"
	$numdays = read-host "How many days of the week do you want to schedule for[# 1 - 7]"
    $daysofweek = @()
	$counter = 0
        while ($counter -lt $numdays){
            $dow = read-host "Enter a number for the day of the week [Sunday, Monday, Tuesday, etc]"
            $daysofweek += ,@($dow)
    $trigger = New-JobTrigger –Weekly –DaysOfWeek $daysofweek –At $time -weeksinterval $weeks
    # The line below is only needed to show the job schedule
    $trigger | fl

if ($frequency -eq "hourly") {
    $hourly = read-host "Enter the number of hours between jobs"
    $startdate = read-host "Enter a start date [i.e. 1/1/2015]"
    $trigger = New-JobTrigger -Once -At "$startdate $time" -RepetitionInterval (New-TimeSpan -Hour $hourly) -RepetitionDuration ([TimeSpan]::MaxValue)
    # The line below is only needed to show the job schedule
    $trigger | fl

if ($frequency -eq "daily") {
    $days = read-host "What is the interval (in days) that the script should run"
    $trigger = New-JobTrigger –daily –At $time -daysinterval $days
    # The line below is only needed to show the job schedule
    $trigger |fl

# This command does the base job of registering this new task.
Register-ScheduledJob -Name $name -FilePath $filepath -Trigger $trigger

A Test Run
The script has three options for scheduling tasks: hourly, daily or weekly.

Here is a simple weekly scheduled script run that runs on Sunday, Monday and Tuesday every three weeks:


Hourly run of the script that repeats running every 5 hours.


Daily run of the script that repeats every 3 days.


The very last line could use some additional parameters (authentication for example), see the Register-ScheduledJob link at the bottom for more information.

I will add this to my lab-setup script 1.4 and upload that soon. I intend to use it for scheduling various tasks to help simula load.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s