Tuesday, March 1, 2016

Create a task in Task Scheduler using PowerShell Script

 

We all know how to create a task in Task Scheduler. When we create a task we need to add many settings to run the task periodically as per the settings. But what to do when we want to create the same task in multiple environments in your project. Creating them manually is a tedious task and as usual Microsoft heard you and provided the option to configure the tasks using the mighty powerful “PowerShell script”. But with one exception that this is available for Windows 8.1 and higher and Windows 2012.

To demonstrate the powerful PowerShell capability, let’s dive in.

I want to create the following task in order to run a testapp.exe periodically.

We want to accomplish the following:

Create a new task and the task name should be “Test Task” and it should run as “NT Authority”.

clip_image001

Figure 1: General properties

Trigger tab should be set with the settings as

1. It should run daily and start date is 3/1/2016 and at 12:00:00 AM and it should recur for every 1 day.

2. Also the task should repeat every one hour and it should run for a duration of 1 hour.

clip_image003

Figure 2: Trigger settings

The action tab should be configured to run the program “TestApp.exe” which is located in C Drive.

clip_image005

Figure 3: Action settings

Our settings tab should be configured with the following settings:

1. Allow task to run on demand

2. If the task fails, restart every 1 hour and it should attempt restarting for 3 times

3. If the task runs for more than 1 hour then stop it and also force it to be stopped if it’s not stopping

clip_image007

Figure 4: Setting tab

Let us write the PowerShell script to accomplish the above mentioned task in the task scheduler. Here are the steps:

Step 1: Name of the task.

Create a variable and assign task name. In our case it should be “Test Task”.

# Name of Task to create

$taskName = "Test Task"

Step 2: Set the credentials.

Create two more variables and assign it the credentials.

In our case, we want the task to run as “NT Authority” and password is not required. But if you want to run with a particular account username and password, create username and password variables and assign them the values.

# UserName to run task as

$user = "NT AUTHORITY\SYSTEM"

# Password for above user

#$password = "userPassword" Uncomment this line if you need to set up password

Step 3: Now create the task

In our case we do not have any arguments. But if you need to add then uncomment the $argument line and set the $argumentvalue

$action defines the action to perform i.e. to execute the exe file and uncomment the argument parameter after the exe if you need to add it.

$trigger defines the trigger settings

$settings defines the settings settings

#$argument = "-Noninteractive -Noprofile -Command &'" + $argumentvalue + "'"

$action = New-ScheduledTaskAction -Execute "C:\MyTestApp.exe" #-Argument $argument

$trigger = New-ScheduledTaskTrigger -Daily -At 12am

$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 60)

$settings.RestartCount=3

$settings.RestartInterval = 'PT01H'

Step 4: Get the instance of scheduled task by specifying New-ScheduledTask command and set the action and trigger and register the scheduled task.

$inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings

Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user

Step 5: Now wait for 5 seconds and then set the trigger repetition interval and duration and set the scheduled task.

Start-Sleep -Seconds 5

$t = get-scheduledtask -TaskName $taskName

$t.Triggers.repetition.Duration = 'PT60M'

$t.Triggers.repetition.Interval = 'PT01H'

$t | Set-ScheduledTask

The completed script will look as below:

# Name of Task to create

$taskName = "Test Task"

#$argumentvalue = "first"

# UserName to run the task created as

$user = "NT AUTHORITY\SYSTEM"

# Password for above user

#$password = "userPassword"

# Create Task

#$argument = "-Noninteractive -Noprofile -Command &'" + $argumentvalue + "'"

$action = New-ScheduledTaskAction -Execute "C:\MyTestApp.exe" #-Argument $argument

$trigger = New-ScheduledTaskTrigger -Daily -At 12am

$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 60)

$settings.RestartCount=3

$settings.RestartInterval = 'PT01H'

$inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings

Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user

Start-Sleep -Seconds 5

$t = get-scheduledtask -TaskName $taskName

$t.Triggers.repetition.Duration = 'PT60M'

$t.Triggers.repetition.Interval = 'PT01H'

$t | Set-ScheduledTask

The above few lines of code will do the job of creating the task with the same settings and you can automate it so that you can run wherever you want.

When you run the script you should see following screen:

clip_image009

Figure 5: Script execution

A trick if you do not know how to set the parameters for the commands. Go to “Windows PowerShell ISE” and run as administrator if needed and type the command that you want to add to the script by using the command window as below:

clip_image011

Figure 6: Command window

You can find more commands in the MSDN and hope this article is useful for creating a basic task in the task scheduler using the PowerShell script. With the growth of automation and agile methodologies everywhere, it would be beneficial for the teams to explore the PowerShell more and more and to replace your manual tasks wherever it is possible.

1 comment:

Developer said...

-ExecutionTimeLimit (New-TimeSpan -Minutes 60) is the piece of code I am really in need of. Thanks !

Post a Comment