Manage Scheduled Tasks in Windows 8 and Windows Server 2012 with PowerShell : Part 2

In the first article of this series, I showed you how to use the ScheduledTasks module to retrieve information about scheduled tasks. This requires PowerShell v3 but is only available on Windows 8 and Windows Server 2012. However, you can use module cmdlets to remotely manage tasks on either platform.
In this article I want to walk through creating a new scheduled task using PowerShell. Eventually we will use the New-ScheduledTask cmdlet, and it is possible to create a task with a complex one-line command. But for the sake of clarity I’m going to break things down step-by-step. Once you understand the concepts and have some experience you can throw everything together in a single command.

Defining a Trigger

The first item of business is to define a scheduled task trigger. That is, when do we want the task to run? The cmdlet to use is New-ScheduledTaskTrigger. You can see the help in Figure 1.
 
New Scheduled Tasks
I am not going to go through every parameter option, as most of the parameters should be self-explanatory. But I do want to point out the –At parameter. You can specify any date or time string, and the cmdlet will try to convert it into an appropriate [DateTime] object.

PS C:\> new-jobtrigger -Once -At "18:00"   Id         Frequency       Time                   DaysOfWeek          Enabled --         ---------       ----                   ----------          ------- 0          Once            10/3/2012 6:00:00 PM                          True PS C:\> new-jobtrigger -Once -At "12/15/2012 8:30"   Id         Frequency       Time                   DaysOfWeek          Enabled --         ---------       ----                   ----------          ------- 0          Once            12/15/2012 8:30:00 AM                         True

Here’s an example of a weekly trigger that is set for Monday, Wednesday and Friday at 11:55 PM.

​PS C:\> new-jobtrigger -Weekly -DaysOfWeek Monday,Wednesday,Friday -at 11:55PM
 
Id        Frequency       Time                   DaysOfWeek           Enabled
--        ---------       ----                   ----------           -------
0         Weekly          10/3/2012 11:55:00 PM  {Monday, Wednesday, ... True

I’ve just been writing these examples to the pipeline so you can see what they look like. For my new task I’ll create a trigger and save it to a variable.

PS C:\> $t=new-scheduledtasktrigger -daily -at 12:00PM

Defining an Action

Next, I need to define what I want my task to do. This is accomplished with the New-ScheduledTaskAction cmdlet.
 
New-ScheduledTask Help
I want to create a task to create an msinfo report for the local computer in the C:\Work directory.

​
PS C:\> $a=new-scheduledtaskaction -execute "msinfo32.exe" -argument "/report c:\work\%computername%-msinfo.txt" -workingdirectory "c:\work"

The value for the –Execute parameter is the path to the program I want to run. Because it is in the system32 directory I can skip using the full path. The –Argument parameter contains the command line arguments. Notice I’m using the legacy environmental variable %Computername% instead of a PowerShell version. Even though I am creating this in PowerShell, the command will actually run in a CMD.EXE session. Don’t forget that all of this is relative to the computer that will run the task: If you are creating a scheduled task on a remote computer, then all files and paths are on THAT computer, not yours.

Specify Credentials

Next, I need to specify the credentials I intend to use for the scheduled task. When I get to registering a task I could simply enter the user name and password as parameter values. But you may want to use variables. You could do this:

PS C:\> $username = "globomantics\administrator" PS C:\> $password = "P@ssw0rd"

If you are working interactively in a secure setting this is probably okay. But another approach to try, especially if you are creating a scheduled task from a script, is to prompt for credentials.

$cred = get-credential "$env:computername\administrator" $username = $cred.username $password = ($cred.GetNetworkCredential()).password

Yes, $password is in clear text but only for as long as your script is running and only in memory, so it should be pretty secure. You should not hardcode any passwords in your script.

Register the Task

Finally, we are ready to register the task using the Register-ScheduledTask cmdlet. To keep things flexible, I’ll assign the task name to a variable.

​
PS C:\> $name="DailyMSInfo"
PS C:\> Register-ScheduledTask -TaskName $name -Trigger $t -Action $a -User $username -Password $password
 
TaskPath                                       TaskName                 State
--------                                       --------                 -----
\                                              DailyMSInfo              Ready

The task will be created in the root folder of the task scheduler as you can see below in Figure 3.
 
newly scheduled task
You can create tasks in other folders in the task scheduler using the –TaskPath parameter.

PS C:\> Register-ScheduledTask -TaskName $name -Trigger $t -Action $a -User $username -Password $password -TaskPath MyCompany\Reporting   TaskPath                                       TaskName                State --------                                       --------                ----- \MyCompany\Reporting\                          DailyMSInfo             Ready

The cmdlet will create the path if it doesn’t exist.

Summary

In only takes a few quick steps to create a scheduled task using PowerShell. If you want to push the task out to remote computers, create all the settings on your computer then register it using the –CIMSession parameter, and specify the names of the Windows 8/Windows Server 2012 system. You can create scheduled tasks using either an interactive console, or a script although be careful about how you handle passwords. In part three of this series, we’ll look at managing this task.